C# - Directory.CreateDirectory() 中的 UnauthorizedAccessException

发布于 2024-12-13 17:03:50 字数 1384 浏览 3 评论 0原文

我是 C# 新手,但我想我在 Directory.CreateDirectory 中使用了一个简单的命令, 虽然我只是在自己的电脑上构建,但一切都很好。但是当我发布它并在其他PC上打开该应用程序时,它抛出了这个异常,并且我在代码源上没有找到如何修复它的答案。 我尝试在Win7中以管理员身份运行它,但也不起作用。 这是代码。它是葡萄牙语的,但我认为这不会成为问题:)

非常感谢大家。

string diretorio = @"C:\Program Files\LAPER\EqNumDPI\Edifícios\" + NomeEdificio;

        if (MessageBox.Show("Você tem certeza de que inseriu os dados corretamente?\nEsses campos não poderão ser alterados posteriormente.",
            "[LAPER] Cálculo do EqNumDPI", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning,
            MessageBoxDefaultButton.Button2) == DialogResult.OK)
        {
            TelaAdicionarAmbiente Tela = new TelaAdicionarAmbiente();
            if (!Directory.Exists(diretorio))
            {
                Directory.CreateDirectory(diretorio);
                StreamWriter file2 = new StreamWriter(@"C:\Program Files\LAPER\EqNumDPI\Edifícios\"+NomeEdificio+"\\metodo.txt", true, Encoding.ASCII);
                if (rBArea.Checked) file2.Write("AREA");
                else file2.Write("ATIVIDADE");
                file2.Close();
                this.Close();
            }
            else
            {
                MessageBox.Show("Nome de edifício já existe.\nPor favor, insira outro nome.",
                    "[LAPER] Cálculo do EqNumDPI",MessageBoxButtons.OK,MessageBoxIcon.Stop);
            }
        }

I'm new at C#, but I guess I'm using a simple command in Directory.CreateDirectory,
and while I was only building in my own PC everything was fine. But when I published it and opened the app in other PC, it has thrown this exception, and I have found no answers on how to fix it on the code source.
I tried to run it as admin in Win7, but didn't work also.
Here's the code. It's in portuguese, but I don't think it'll be a problem :)

Thank you all so much.

string diretorio = @"C:\Program Files\LAPER\EqNumDPI\Edifícios\" + NomeEdificio;

        if (MessageBox.Show("Você tem certeza de que inseriu os dados corretamente?\nEsses campos não poderão ser alterados posteriormente.",
            "[LAPER] Cálculo do EqNumDPI", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning,
            MessageBoxDefaultButton.Button2) == DialogResult.OK)
        {
            TelaAdicionarAmbiente Tela = new TelaAdicionarAmbiente();
            if (!Directory.Exists(diretorio))
            {
                Directory.CreateDirectory(diretorio);
                StreamWriter file2 = new StreamWriter(@"C:\Program Files\LAPER\EqNumDPI\Edifícios\"+NomeEdificio+"\\metodo.txt", true, Encoding.ASCII);
                if (rBArea.Checked) file2.Write("AREA");
                else file2.Write("ATIVIDADE");
                file2.Close();
                this.Close();
            }
            else
            {
                MessageBox.Show("Nome de edifício já existe.\nPor favor, insira outro nome.",
                    "[LAPER] Cálculo do EqNumDPI",MessageBoxButtons.OK,MessageBoxIcon.Stop);
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2024-12-20 17:03:50

此异常通常意味着程序运行所用的帐户(默认情况下为登录用户的帐户)没有创建指定目录的正确权限。

MSDN 在 < 的文档中对 UnauthorizedAccessException 有这样的说法代码>Directory.CreateDirectory

调用者没有所需的权限。

授予该帐户正确的权限,一切都会好起来的。

This exception normally means that the account that the program runs under (by default the logged in user's account) does not have the correct permissions to create the named directory.

MSDN has this to say about UnauthorizedAccessException in the documentation for Directory.CreateDirectory:

The caller does not have the required permission.

Grant the account the correct permissions and all will be well.

笑忘罢 2024-12-20 17:03:50

如果这不是 Oded 的答案,那么问题可能是 exe 文件的位置。如果它是从网络卷运行,则其权限会较低。

您应该研究强名称密钥,使用它们来签署您的应用程序,并与您的网络管理员合作以“信任”由该强名称密钥签名的程序集。

参考:

http://msdn.microsoft.com/ en-us/library/h4fa028b(v=vs.80).aspx

另外 - 如果您使用的是 Vista 或 Windows 7,您的应用程序将无法在Program Files 目录,除非您以管理员身份运行它。

If it's not Oded's answer, then the issue could be the location of the exe file. If it's being run from a network volume, its permissions will be lower.

You should look into strong-name keys, using them to sign your applications, and working with your network administrator to "trust" assemblies signed by that strong name key.

Reference:

http://msdn.microsoft.com/en-us/library/h4fa028b(v=vs.80).aspx

Also - if you're on Vista or Windows 7, your app won't be able to write/create anything in the Program Files directory unless you run it as an administrator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文