简体   繁体   English

UnauthorizedAccessException错误

[英]UnauthorizedAccessException Error

Fellow User, Trying to write to a file - from a console application in Visual C# - using System.IO, when I encountered this error: UnauthorizedAccessException unhandled (which was not present in the courseware I was watching). 资深用户,当我遇到以下错误时,尝试使用System.IO从Visual C#中的控制台应用程序写入文件: UnauthorizedAccessException unhandled (我正在观看的课件中不存在)。 I am aware that this is quite a simple problem and have looked for the solution on msdn.com yet all I was able to find was that my problem was a security error or System.IO error, nothing about how to fix the problem. 我知道这是一个非常简单的问题,已经在msdn.com上寻找了解决方案,但我所能找到的只是我的问题是安全错误或System.IO错误,与如何解决该问题无关。

This is the code: 这是代码:

string someText = "I want to write this to a text file!";

File.WriteAllText(@"C:\Users\Keagan\Desktop\
    C#projects\UnderstandingNamespaces", someText);

Regards K.Roe 此致K.Roe

I have faced exactly the same problem. 我遇到了完全相同的问题。 You have two solution: 您有两种解决方案:

First One: Make sure that you have full permission on the folder. 第一个:确保您对该文件夹具有完全权限。

Go to folder -> Right click -> Properties -> Security Tab -> Select user -> Edit -> Allow Full Control.

Second Solution (Most Probably): 第二种解决方案(最有可能):

string someText = "I want to write this to a text file!";

make sure that you someText variable hold the file name with extension. 确保您的someText变量包含扩展名的文件名。 for exmaple: MyFile.txt or something like that. 例如: MyFile.txt或类似的东西。 In addition to that, make sure that you filePath is correct. 除此之外,请确保您的filePath正确。

I faced the same problem, but I don't know why visual studio throw this strange exception. 我遇到了同样的问题,但是我不知道为什么Visual Studio会抛出这个奇怪的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM