Reputation: 93
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.
Upvotes: 0
Views: 155
Reputation: 6783
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.
Upvotes: 1
Reputation: 948
If you have trouble writing authority "run as Administrator "
string rootPath = @"C:\FileSystem\";
string fileName = "data.txt";
if (!Directory.Exists(rootPath))
Directory.CreateDirectory(rootPath);
File.AppendAllText(Path.Combine(rootPath , fileName), "Test");
File.AppendAllText(Path.Combine(rootPath , fileName), "Test2");
For a bottom row > Environment.NewLine > \n
File.AppendAllText(Path.Combine(rootPath , fileName), "Test2"+Environment.NewLine);
Upvotes: 0