简体   繁体   中英

WriteAllBytes to an “in program” created directory Access to path Denied

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);

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM