Click here to Skip to main content
Sign Up to vote bad
good
See more: VB.NET
In my program xPro I am having problems with it using files in it's directory in c:\Program Files(x86)\xPro. In one form it rights data to a .txt file when it closes. Everytime I try to close the program it says that access to the .txt file is denied. Any help?
 
Here is the code for Form Closing:
TextBox2.Text = ""
        For Each thing As Object In ListBox1.Items
            TextBox2.AppendText(Trim(thing.ToString & vbNewLine))
 
        Next
        
        Dim SR As New StreamWriter(My.Settings.MyDirectory + "\Friends.txt")
        SR.Flush()
        SR.Close()
        Dim SR2 As New StreamWriter(My.Settings.MyDirectory + "\Friends.txt")
        SR2.Write(TextBox2.Text)
        SR2.Close()
 
I do now realize that I should add a Try statement but eitherway that doesn't exactly fix the problem.
 

************** Exception Text **************
System.UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\xPro\Friends.txt' 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, Boolean useLongPath)
   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 xPro.FL.FL_FormClosing(Object sender, FormClosingEventArgs e)
   at System.Windows.Forms.Form.OnFormClosing(FormClosingEventArgs e)
   at System.Windows.Forms.Form.WmClose(Message& m)
   at System.Windows.Forms.Form.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)
Posted 4 Oct '12 - 20:01
Edited 4 Oct '12 - 20:03


2 solutions

On Windows7 the "Program Files" folder is protected by the UAC, so to write there you need administrator privileges (and the UAC confirmation).
 
It is better to write "data" to the user's local profile folders instead, use the Environment variable for this : http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^]
  Permalink  
Access to the path 'C:\Program Files (x86)\xPro\Friends.txt' is denied.
Proper security privileges are not in place. You need to make sure that write permissions are in place. Further, if the text file is marked readonly, you cannot edit it until it is writable.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 514
1 CPallini 245
2 Mahesh Bailwal 244
3 Maciej Los 240
4 Aarti Meswania 213
0 Sergey Alexandrovich Kryukov 9,162
1 OriginalGriff 7,179
2 CPallini 3,913
3 Rohan Leuva 3,176
4 Maciej Los 2,588


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid