2013-03-02 6 views
0

로그 파일을 만들어야하는 작은 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 

아무도가하십시오 - 나는 기능이 지금 특히 작동하지 않습니다 실현하지, 난 그냥 해요 먼저 파일을 만들려고 할 때 그 파일에 대해 작업하겠습니다.

+0

오류에'사용하지 마십시오을 해결하는 것이 만족의 try...catch 방법을 통합 한 '. 이것은 VB6이 아닙니다. : – Ryan

+0

내가 오류를 포착하기 위해 사용하는 것에 대한 암시는 무엇입니까? 나는 일반적인 연습 인 VBA에 익숙합니다. –

+0

A Try 'Catch' 블록. 좀 더 구체적으로 만들고'Using' 블록을 사용하십시오. 당신의 스트림이 적절히 처리되도록하거나,'File.ReadLines'도 작동합니다. 그러나 오류 처리를 완전히 제거하고 오류가 어디서 발생했는지 정확히 알아 내십시오 *. * 정확한 경로인지 확인하십시오 – Ryan

답변

0

@RubensFarias의 답변 덕분에 in this thread 게시 됨이 문제를 해결할 수있었습니다. 내 origianl 질문에 대한 코멘트에 @minitech에 의해 제안

나 또한 그렇게 잘하면 그는 내가 그의 관심사 :

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 
+3

"DateTime.Today.ToString ("dd-mm-yyyy ")'에 이상한 것을 발견 할 수 있습니다 -"mm "은 분을 의미하기 때문에" MM "(월을 의미) –

+0

고맙습니다. 나는 그 사실을 알지 못했지만 실제로는 '00'만 출력했습니다. –