【问题标题】:access denied when saving a file.exe to my documents将 file.exe 保存到我的文档时访问被拒绝
【发布时间】:2013-06-08 15:51:49
【问题描述】:

您好,我想创建一个程序,将文件保存到我的文档\test。 该文件是.exe。由于某种未知原因,我收到拒绝访问错误,当我测试程序试图保存 .txt 文件(使用 StreamWriter)时,程序运行没有任何问题。 请各位大侠帮帮我。

以下代码抛出错误

byte[] myfile = Properties.Resources.WallPaper;
        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string path2 = path + @"\" + "test";
        System.IO.Directory.CreateDirectory(path2);
        File.WriteAllBytes(path2, myfile);

以下代码在保存 .txt 文件时可以正常工作

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string path2 = path + @"\" + "test";
        System.IO.Directory.CreateDirectory(path2);
        StreamWriter file = new StreamWriter(path2 + @"\" + "text.txt");

【问题讨论】:

  • 您能否发布您遇到的异常,以便我们获得更好的图片
  • unauthorizedAccessExepetion - 访问路径“C:\Users\michael\Documents\test”被拒绝

标签: c# byte access-denied


【解决方案1】:

你的两个例子不一样。您的第一个示例不起作用,因为System.IO.Directory.CreateDirectory 已经创建了名为test 的目录。您不能像文件一样覆盖该目录。为了使您的示例 1 表现得像示例 2:

System.IO.Directory.CreateDirectory(path2)
File.WriteAllBytes(path2 + @"\" + "text.txt", myfile)

【讨论】:

  • 我不明白为什么......当我尝试将文件保存到 D 时它工作正常
  • @misha312 在目录中写入文件与在现有目录上写入文件不同。作为一个练习,您可以了解我的观点,创建一个名为 Test 的文件(手动,而不是通过您的程序),然后尝试将该 Test 文件复制并粘贴到已经有一个名为 Test 的目录的文件夹中.
【解决方案2】:

我从未尝试写入exe文件,但您已经尝试设置权限?我不知道在 c# 中是否存在一些方法可以做到这一点,但肯定存在“exec”,所以你可以用它来发送 chmod 命令

【讨论】:

    猜你喜欢
    • 2020-07-26
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多