我创建了一个脚本,用于将可执行文件从 Resources
提取到我的桌面。这在我的机器上有效,但由于用户名不同而无法在其他人的机器上使用。以下脚本完美运行:
private void button1_Click(object sender, EventArgs e)
{
byte[] myfile = Properties.Resources.SOMETHING;
File.WriteAllBytes("C:\\Users\\Alex\\Desktop\\SOMETHING.exe",myfile);
}
我做了一些研究,发现我需要使用
(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
所以我编译了这个脚本:
private void button1_Click(object sender, EventArgs e)
{
byte[] myfile = Properties.Resources.SOMETHING;
File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),myfile);
}
问题是它并没有说我的代码有错误,但是当我运行它并按下按钮时,我得到以下错误:
System.UnauthorizedAccessException Message=Access to the path 'C:\Users\Alex\Desktop' is denied.
我试过以管理员权限运行代码,但这也没有帮助。
最佳答案
在读取文件之前使用 File.SetAttributes(myfile, FileAttributes.Normal);
属性,它应该可以工作。
关于c# - 将文件提取到桌面时为 "System.UnauthorizedAccessException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55091190/