[英]UnauthorizedAccessException when Writing to a file using TextWriter in C#
I have an error say:我有一个错误说:
"Access to the path 'C:\\Program Files (x86)\\My Program\\bin\\Debug\\myName.data' is denied"
“访问路径 'C:\\Program Files (x86)\\My Program\\bin\\Debug\\myName.data' 被拒绝”
This is my code这是我的代码
TextWriter tw = new StreamWriter("bin\\Debug\\myName.data");
tw.Write(txtLoginName.Text);
tw.Close();
I have give full Control permission for my all project files.我已为我的所有项目文件授予完全控制权限。 I make installer so that Clients install it in their pc, when when checking the files, I found there is no write access permission given to users.
我制作安装程序,以便客户将其安装在他们的电脑上,当检查文件时,我发现没有给用户写访问权限。 How to handle this?
如何处理?
************** Exception Text **************
System.UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\My Program\bin\Debug\myName.data' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path)
at clientChat.Form2.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Installers will run in the user's login context, and normal user's of a system do not have permissions to write to the Program Files or other such system folders.安装程序将在用户的登录上下文中运行,系统的普通用户没有写入程序文件或其他此类系统文件夹的权限。
You need to move the location of the folder to the User's App data folder.您需要将文件夹的位置移动到用户的应用程序数据文件夹中。 Or Some other common location created by your installer.
或由您的安装程序创建的其他一些常见位置。
Program files (and other locations in system folders) are not writeable by normal users - by design.普通用户无法写入程序文件(以及系统文件夹中的其他位置) - 按照设计。
Please use document folder or other per-user location to store user's data.请使用文档文件夹或其他每个用户的位置来存储用户的数据。 The Environment.GetFolderPath lets you get correct locations for current user, consider which of Environment.SpecialFolder works for your case and use it as base folder.
Environment.GetFolderPath可让您获取当前用户的正确位置,考虑哪个Environment.SpecialFolder适用于您的情况并将其用作基本文件夹。
Followng sample will give path in the "my documents" folder:以下示例将在“我的文档”文件夹中给出路径:
var pathToFile = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Personal),
"my_file_name.txt");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.