![](/img/trans.png)
[英]Access to path is denied when trying to import from the client's desktop with SSIS
[英]Permission denied when trying to create file on users desktop
我有一个小型VB应用程序,需要创建一个日志文件,理想情况下,我希望在用户桌面上看到它,因为它很容易看到。
但是,当我尝试创建文件(* .txt文件)时,出现以下错误-
mscorlib.dll中发生类型'System.UnauthorizedAccessException'的第一次机会异常
现在,很明显这是一个权限问题,但是我不知道如何解决它,所以我想知道是否有人可以帮助我? 谢谢。
用于创建文件的完整代码(取自MSDN,并进行了很小的修改)-
Imports System
Imports System.IO
Imports System.Text
Module write_text
Public Class Log_File
'*'
' Createa a log file on the users desktop and adds the 'output' text to it
'
' @param required string output The text to output to the log
'*'
Public Shared Sub write_to_file(ByVal output As String)
Dim path As String ' The path to where the file to create and write should be saved
Dim file_name As String ' The name of the log file that is to be created
Debug.Print(vbCrLf & " write_to_file(){" & vbCrLf)
' Set the error handler
On Error GoTo ExitError
path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
file_name = "MusicRenaming - " & Date.Today & ".txt"
Debug.Print(" Log file: " & path & "\" & file_name)
' Check if the file exists
If File.Exists(path) = False Then
' Create a file to write too.
Dim sw As StreamWriter = File.CreateText(path)
sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
sw.Flush()
sw.Close()
Debug.Print(" File had to be created")
End If
' Open the file to write too
Dim sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Debug.Print(" Output to file complete")
Loop
' Close the file
sr.Close()
Debug.Print(vbCrLf & " }")
ExitError:
If Err.Number <> 0 Then functions.error_handler()
End Sub
End Class
End Module
请不要 -我意识到该功能目前尚不完善,我只是想先创建文件,然后再进行处理。
感谢@RubensFarias 在此线程中发布的答案,我得以解决此问题。
我也将@minitech建议的try...catch
方法并入了我的origianl问题的注释中,因此希望他对我解决了他的担忧感到满意:)
Imports System.IO
Module write_text
Public Class Log_File
'*'
' Createa a log file on the users desktop and adds the 'output' text to it
'
' @param required string output The text to output to the log
'*'
Public Shared Sub write_to_file(ByVal output As String)
Dim file_name As String ' The name of the log file that is to be created
Dim file_path As String ' The full path to where the file to create and write should be saved
Dim file_exists As Boolean ' Whether or not the file already exists
Try
file_name = "MusicRenaming - " & DateTime.Today.ToString("dd-mm-yyyy") & ".txt"
file_path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) & "\" & file_name
file_exists = File.Exists(file_path)
Using sw As New StreamWriter(File.Open(file_path, FileMode.OpenOrCreate))
sw.WriteLine(output)
End Using
Catch oError As Exception
functions.error_handler(oError)
End Try
End Sub
End Class
End Module
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.