2017-08-29 100 views
-1

我有以下方法/步驟如何創建和打開文件

Private Sub CreateFile(ByVal filename As String, ByVal directory As String, ByVal extension As String) 

    Dim file2create = directory & filename & extension 
    Console.WriteLine(file2create) 
    If (Not File.Exists(file2create)) Then 
     File.Create(file2create) 
    Else 
     File.Open(file2create, FileMode.Open) 
    End If 
    console.ReadLine() 
End Sub 

此代碼成功地創建了一個文件,但無法打開它,只是說,我查過其他的答案類似的文件問題,但說答案還沒有解決我的問題,我在這裏做錯了什麼。謝謝

================================ UPDATE ============ ========================

謝謝你們的答案,但文件沒有打開。只是爲了澄清,當我說開放時,我的意思是同樣的行爲是物理上去到文件並點擊它。

如果我是從這個question

Private Function ShellExecute(ByVal File As String) As Boolean 

     Dim myProcess As New Process 
     myProcess.StartInfo.FileName = File 
     myProcess.StartInfo.UseShellExecute = True 
     myProcess.StartInfo.RedirectStandardOutput = False 
     myProcess.Start() 
     myProcess.Dispose() 

    End Function 

該文件將成功地打開使用此代碼,但如果我是用file.open()的文件打不開,所以在本質上,我m尋找創建,然後以上述代碼相同的方式打開一個文件

+1

難道你不想只是將'File.Open'移到if語句之外嗎? – PeterJ

+2

或用'File.Open(file2create,FileMode.OpenOrCreate)'替換整個if語句。 –

+0

'File.Create()'和'File.Open()'都返回一個'FileStream',它將鎖定文件被其他代碼/進程訪問。這是你想要的還是你打算用這個代碼實際讀取/寫入它?否則,你應該處理流:'File.Open(file2create,FileMode.OpenOrCreate).Dispose()'。 –

回答

1

你必須在IF..Then塊後移動File.Open

這裏:

Private Sub CreateFile(ByVal filename As String, ByVal directory As String, ByVal extension As String) 
    Dim file2create = directory & filename & extension 
    Console.WriteLine(file2create) 
    If (Not File.Exists(file2create)) Then 
     File.Create(file2create) 
    End If 
    'if file exits then open the file 
If System.IO.File.Exists(file2create) = True Then 
    Process.Start(file2create) 
Else 
    MsgBox("File Does Not Exist") 
End If 
End Sub 
+0

如果我這樣做,我得到這個錯誤----> System.IO.IOException:'進程無法訪問文件'C:\ Users \ jamisco \ Documents \ jbiadfsdipa。txt',因爲它正在被另一個進程使用。' – Jamisco

+0

等待,你的意思是字面上打開文件?大聲笑沒關係。看到我的更新回答 – Subaz

+0

我現在質疑我的開放的定義 – Jamisco

3

這裏是正確的方式來創建一個文件,如果它不存在,然後再打開它使用File.Open方法,它返回一個FileStream對象:

Private Sub OpenOrCreateFile(ByVal filename As String, ByVal directory As String, ByVal extension As String) 
    'Use `Path.Combine` so you don't have to worry if the 
    'directory path ends with a "\" or not. 
    Dim file2create = Path.Combine(directory, filename) & extension 
    Console.WriteLine(file2create) 

    'Use a `Using` statement to make sure the FileStream object gets 
    'disposed, and to prevent the file from staying locked after 
    'finishing what you want to do with it. 
    Using fs As FileStream = File.Open(file2create, FileMode.OpenOrCreate) 
     'I'm not sure what you'd like to do after opening the file, 
     'but now you have a `FileStream` object which you can use 
     'to write bytes to the file (i.e. using `fs.Write()`). 
    End Using 

    Console.ReadLine() 
End Sub 

如果你只是想創建一個文件,並寫一些文本覆蓋現有的內容,你可以使用:

File.WriteAllText(file2create, "SomethingToWrite") 

希望有所幫助。

+0

這就是應該如何做。謝謝。看着你的代碼,我也學到了一些東西。Path.Combine。現在走開來更新大量的代碼。 –

相關問題