Reputation: 329
I have an error say:
"Access to the path 'C:\Program Files (x86)\My Program\bin\Debug\myName.data' is denied"
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)
Upvotes: 1
Views: 1224
Reputation: 100620
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.
Followng sample will give path in the "my documents" folder:
var pathToFile = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Personal),
"my_file_name.txt");
Upvotes: 0
Reputation: 4376
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.
Upvotes: 1