简体   繁体   中英

“System.UnauthorizedAccessException” when extracting file to Desktop

I created a script to extract an executable file from the Resources to my Desktop. This works on my machine, but won't work on other's people machines because of a different username. The following script works perfectly:

private void button1_Click(object sender, EventArgs e)
{
    byte[] myfile = Properties.Resources.SOMETHING;
    File.WriteAllBytes("C:\\Users\\Alex\\Desktop\\SOMETHING.exe",myfile);
}

I did some some research and found that I need to use the (Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

So I compiled this script:

private void button1_Click(object sender, EventArgs e)
{
    byte[] myfile = Properties.Resources.SOMETHING;
    File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),myfile);
}

The problem is that it doesn't say there is an error in my code, but when I run it and press on the button I get the following error:

System.UnauthorizedAccessException Message=Access to the path 'C:\\Users\\Alex\\Desktop' is denied.

I've tried running the code with administrator rights, but that also didn't help.

Use File.SetAttributes(myfile, FileAttributes.Normal); property before reading the file, it should work.

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