1

I am trying to read the binary contents of a file with File.ReadAllBytes and getting the exception that access to the file is denied. Do I have to open the file first? Its taking the exception path in this try-catch and displaying.

Unable to load configuration data. Access to the path 'c:\worl\Project Alpha\Code\AlphaBackendService\AlphaBackendService\bin\Debug\alphaService.xml' is denied.

What am I missing?

try
{
  string path = AppDomain.CurrentDomain.BaseDirectory;
  eventLog1.WriteEntry(path);
  string fileName = System.IO.Path.Combine(path, "alphaService.xml");

  string sKey = "LvtZELDrB394hbSOi3SurLWAvC8adNpZiJmQDJHdfJU=";
  Byte[] keyBytes = Convert.FromBase64String(sKey);

  Byte[] contentsBytes = File.ReadAllBytes(fileName);

  string xmlStr = Encoding.UTF8.GetString(contentsBytes); 

  DecryptStringFromBase64String(xmlStr, keyBytes);

  eventLog1.WriteEntry(xmlStr);

  using (XmlReader reader = XmlReader.Create(new StringReader(xmlStr)))
  {
    reader.ReadToFollowing("DatabaseServerName");
    DatabaseServerName = reader.ReadElementContentAsString();
    reader.ReadToFollowing("DatabaseUserName");
    DatabaseUserName = reader.ReadElementContentAsString();
    reader.ReadToFollowing("DatabasePassword");
    DatabasePassword = reader.ReadElementContentAsString();
    reader.ReadToFollowing("RegistrationCode");
    RegistrationCode = reader.ReadElementContentAsString();
  }
  eventLog1.WriteEntry("Configuration data loaded successfully");
}
catch (Exception ex)
{
  eventLog1.WriteEntry("Unable to load configuration data.  " + ex.Message);
}

The Decrypt function requires a string with the contents but it does a Convert.FromBase84String so I don't know if File.ReadAllBytes should be used.

static string DecryptStringFromBase64String(string cipherText, byte[] Key)
    {
        // Check arguments. 
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");

        string plaintext = null;
        // this is all of the bytes
        var allBytes = Convert.FromBase64String(cipherText);
        // get our IV that we pre-pended to the data
        byte[] iv = new byte[KeySize / 16];
        Array.Copy(allBytes, iv, iv.Length);
        // get the data we need to decrypt
        byte[] cipherBytes = new byte[allBytes.Length - iv.Length];
        Array.Copy(allBytes, iv.Length, cipherBytes, 0, cipherBytes.Length);

        using (var aes = Aes.Create())
        {
            // Create a decrytor to perform the stream transform.
            var decryptor = aes.CreateDecryptor(Key, iv);

            // Create the streams used for decryption. 
            using (MemoryStream msDecrypt = new MemoryStream(cipherBytes))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        // Read the decrypted bytes from the decrypting stream 
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }
        }

        return plaintext;
    }
user2471435
  • 1,644
  • 7
  • 35
  • 62
  • 2
    that means you need certain level of privilege to access the file – HackerMan Apr 08 '14 at 17:07
  • I was able to access the file just fine when it was plain XML. – user2471435 Apr 08 '14 at 17:09
  • I checked and its not a permission problem. – user2471435 Apr 08 '14 at 17:24
  • Can you copy the file from the command line? If so, is the program running with the same privileges as you? It's likely that you have a permissions problem. Other possibility is that some other thread or process has the file open in exclusive mode. – Jim Mischel Apr 08 '14 at 17:24
  • 1
    File format has nothing to do with file access. – Peter Ritchie Apr 08 '14 at 17:24
  • `Unable to load configuration data. Access to the path 'c:\worl\Project Alpha\Code\AlphaBackendService\AlphaBackendService\bin\Debug\alphaService.xml' is denied` is contradicting your assumption of "not a permission problem". – Peter Ritchie Apr 08 '14 at 17:25
  • Has something *else* opened the file denying reads? – Peter Ritchie Apr 08 '14 at 17:26
  • I was able to copy the file with the command line just fine. I can't see where anyone would have it open. Are sure its not because its an encrypted file? Do I have my code correct above? – user2471435 Apr 08 '14 at 17:29
  • Use a tool such as Process Explorer and look for the file handle to check if it is not open. Sometimes programs forget to clean up their handles. – Maarten Bodewes Apr 13 '14 at 20:23

0 Answers0