WriteAllBytes to an "in program" created directory Access to path Denied

5.1k views Asked by At

Hi made a little installer in C# with app.manifest parametter to AdministratorRequired to be able to edit the computer freely, but when I execute this code:

System.IO.Directory.CreateDirectory(cheminInstall.Text);
File.WriteAllBytes(cheminInstall.Text, Properties.Resources.Sara);
2

There are 2 answers

8
Aniket Inge On

You are confusing Directory with file. You can write to a file, and save it into a directory. But can't write directly into a directory.

cheminInstall.Text = System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)+"\\Storationer";
System.IO.Directory.CreateDirectory(cheminInstall.Text);
File.WriteAllBytes(cheminInstall.Text+"\\YourFileName.txt", Properties.Resources.Sara);
2
David Heffernan On

Look at the code:

System.IO.Directory.CreateDirectory(cheminInstall.Text);
File.WriteAllBytes(cheminInstall.Text, ...);

Well, you create a directory, and then try to write a file with the same name as the directory. That's not possible. You can write a file in a directory. You can overwrite an existing file with a new file. But you cannot write a file to a path that specifies a directory.

I don't know what you mean to do. But presumably you wish to create a directory, and write a file inside that directory. Which is going to require you to pass a path to WriteAllBytes that specifies an object within the directory that you create.