0

嗨,我想创建一个将文件保存到我的文档\测试的程序。该文件是.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");
4

2 回答 2

2

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

System.IO.Directory.CreateDirectory(path2)
File.WriteAllBytes(path2 + @"\" + "text.txt", myfile)
于 2013-06-08T07:45:54.283 回答
0

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

于 2013-06-08T07:47:20.487 回答