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). 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.
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
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. for exmaple: MyFile.txt
or something like that. In addition to that, make sure that you filePath
is correct.
I faced the same problem, but I don't know why visual studio throw this strange exception.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.