开发者

Accessing Text File in Non-Admin mode

开发者 https://www.devze.com 2023-01-08 05:09 出处:网络
I have a text file in Program Files. I cannot write it from a C# application while running in non-admin mode开发者_JAVA技巧.

I have a text file in Program Files. I cannot write it from a C# application while running in non-admin mode开发者_JAVA技巧.

I am using this snippet

TextReader read = new StreamReader("C:\Program Files\......\link");

It is throwing an error that the access to the file is denied, however I can read it!

Thanks


Access to a file can be different for reading and writing. As a non-admin, it's normal to be able to read files in Program Files, but not be able to write them.

If the file is a setting for the current user, you should put it in a folder under the AppData folder. You can find the AppData folder's location by calling Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))

If the file is a setting for all users on the computer, use Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))

See http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx for a list of other possible special folder locations.


Non-admin users don't have write access to files in C:\Program Files by default. If you want to write to a file that is accessible to all users, you should create it in C:\ProgramData.


In .net there is a class File through which you can use the method File.read(file path) this return a string so you can easily manage it it also works in a non admin mode

0

精彩评论

暂无评论...
验证码 换一张
取 消