로그 파일을 만들어야하는 작은 VB 응용 프로그램이있어서 쉽게 볼 수 있기 때문에 사용자 데스크톱에서 사용하고 싶습니다. 그러나사용자가 데스크톱에 파일을 만들려고 할 때 권한이 거부되었습니다.
, 나는 파일 (A * .txt 파일), 나는 다음과 같은 오류 얻을 만들려고 할 때 -
유형 'System.UnauthorizedAccessException'형식의 첫째 예외가 mscorlib.dll에서 에서 발생을
이제는 사용 권한 문제인 것처럼 보이지만 해결 방법을 모르므로 누군가 나를 도울 수 있는지 궁금해합니다. 감사.
는 파일을 생성전체 코드 (MSDN에서 가져온, 그리고 아주 약간 modifided) -
는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
아무도가하십시오 - 나는 기능이 지금 특히 작동하지 않습니다 실현하지, 난 그냥 해요 먼저 파일을 만들려고 할 때 그 파일에 대해 작업하겠습니다.
오류에'사용하지 마십시오을 해결하는 것이 만족의
try...catch
방법을 통합 한 '. 이것은 VB6이 아닙니다. : – Ryan내가 오류를 포착하기 위해 사용하는 것에 대한 암시는 무엇입니까? 나는 일반적인 연습 인 VBA에 익숙합니다. –
A Try 'Catch' 블록. 좀 더 구체적으로 만들고'Using' 블록을 사용하십시오. 당신의 스트림이 적절히 처리되도록하거나,'File.ReadLines'도 작동합니다. 그러나 오류 처리를 완전히 제거하고 오류가 어디서 발생했는지 정확히 알아 내십시오 *. * 정확한 경로인지 확인하십시오 – Ryan