Cem Aytekin Cem Aytekin - 1 year ago 80
C# Question

Creating a New Directory and Writing a File in it

DirectoryInfo di = Directory.CreateDirectory(precombine); // I am creating a new directory.

BinaryWriter write = new BinaryWriter(File.Open(di.FullName, FileMode.Create)); // I want to open a file in it

write.Write(buffer); // and then I want to write in it.

However, I get an error of not to have any permission to write in it. How can I create a new directory for a user and then write the data of the user in it ? Thanks.

Answer Source

Looks like you may be missing the filename - "di.FullName" will give the full pathname of the directory you have created - File.Open needs a file name.

BinaryWriter write = new BinaryWriter(File.Open(Path.Combine(di.FullName, <FILE NAME HERE>), FileMode.Create));

In your code, File.Open will be attempting to create a file with the same name as the directory you have just created - so you don't have permission.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download