将文件解压缩到桌面时出现“ System.UnauthorizedAccessException”

时间:2019-03-10 18:55:04

标签: c# .net winforms

我创建了一个脚本,用于将可执行文件从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 =对路径'C:\ Users \ Alex \ Desktop'的访问被拒绝。

我尝试使用管理员权限运行代码,但这也无济于事。

1 个答案:

答案 0 :(得分:0)

在读取文件之前使用class SentenceIterator: def __init__(self, filepath): self.filepath = filepath def __iter__(self): for line in open(self.filepath): yield line.split() sentences = SentenceIterator('datadir/textfile.txt') model = Word2Vec(sentences) 属性,它应该可以工作。