SWeko
SWeko

Reputation: 30882

Saving a file to a user-created folder

I have (more or less) the following code:

private static void SaveFolder(MyFolder folder, string location)
{
  string folderName = Path.Combine(location, folder.Name);
  DirectoryInfo di = Directory.CreateDirectory(folderName);
  foreach ( MyFile childItem in folder.Children )
  {
    string fileName = Path.Combine(di.FullName, childItem.Name);
    byte[] payload = GetFilePayload(childItem);
    File.WriteAllBytes(fileName, payload);
  }
}

The application is an office add-in, running under a user that is admin on the machine (WinXP)

I create the folder without problems, however when I create the files (File.WriteAllBytes line) I get a UnauthorizedAccessException.

Why can't I save file in a folder I just created?

Upvotes: 1

Views: 1410

Answers (1)

SWeko
SWeko

Reputation: 30882

Sorry, folks, it was a stupid bug in some other code, that essentially transformed

File.WriteAllBytes(fileName, payload);

into

File.WriteAllBytes(folderName, payload);

:(

The OS was trying to save a file with the same name as the folder, and the WTF is the exception I was receiving, that send me on a tangent.

Upvotes: 1

Related Questions