" mscorlib.dll中发生了"System.UnauthorizedAccessException".尝试在C:\驱动器中创建文件时 [英] " 'System.UnauthorizedAccessException' occurred in mscorlib.dll" while trying to create file in the C:\ drive

查看:72
本文介绍了" mscorlib.dll中发生了"System.UnauthorizedAccessException".尝试在C:\驱动器中创建文件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做这个项目,在那里我获取了一些信息并将其保存到文件中.尝试创建该文件时,总是出现System.UnauthorizedAccesssException错误.我尝试添加清单文件并要求管理员,但仍然无法正常工作.我还发布了它,并通过\bin文件夹运行了该程序,但它仍然无法正常工作.

I'm doing this project where I grab some information and save it to a file. While trying to create that file, I always get an System.UnauthorizedAccesssException error. I've tried adding a manifest file and requiring admin, still doesn't work. I've also released it and ran the program through the \bin folder, and it still didn't work.

这是我的代码:

public static void CreateFile(string path, string name)
{
    Debug.WriteLine("Starting to create file...");
    string Name = name;
    string path2 = path;
    string fullPath = Path.Combine(path2, Name);
    using (FileStream fs = new FileStream(fullPath, FileMode.Create))
    {
        Debug.WriteLine("File Created");
    }
}

我也尝试过访问Microsoft网站并使用他们的文件创建方法,但仍然无法正常工作,我遇到了同样的错误!

I also tried going to the Microsoft website and using their method of creating a file, and it still didn't work, I got the same error!

问题是,我如何授予访问程序的权限以写入C:\驱动器?

Question is, how I give access to the program in order to write to the C:\ drive?

推荐答案

默认情况下,大多数用户帐户缺少直接写入C:\驱动器的必要访问权限. UnauthorizedAccessException例外表示在这种情况下.

By default most user accounts lack the necessary access to write directly to the C:\ drive. The exception, UnauthorizedAccessException, indicates that this is the case here.

要进行此工作,您将需要

To make this work you're going to need to either

  1. 选择一个更标准的位置来写入文件
  2. 授予该程序用来运行C:\驱动器的帐户.
  1. Pick a more standard location to write the file
  2. Give the account under which this program runs access to the C:\ drive.

到目前为止,两个#1都是更可取的选择

Of the two #1 is by far the more preferable option

这篇关于" mscorlib.dll中发生了"System.UnauthorizedAccessException".尝试在C:\驱动器中创建文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆