本文介绍了程序无法访问程序文件中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的程序xPro中,我在使用c:\ Program Files(x86)\ xPro的目录中的文件时遇到问题.在一种形式下,它会在关闭时将数据版权授予.txt文件.每次我尝试关闭程序时,都会说拒绝对.txt文件的访问.有帮助吗?
以下是表单关闭的代码:
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()
我现在确实意识到我应该添加一个Try语句,但是无论如何都不能完全解决问题.
**************异常文本**************
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)
推荐答案
这篇关于程序无法访问程序文件中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!