2013-10-10 66 views
0

我有點困惑如何做到這一點。我想要做的是當我點擊Button1,我的程序將在資源管理器中打開一個文件夾,第二個按鈕將打開一個文件作爲文本文件。按鈕,打開應用程序路徑+特定文件夾

這裏是我的代碼:

按鈕1個

Process.Start("explorer.exe", Application.ExecutablePath + "\mvram.biz") 

按鈕2

Process.Start("Notepad.Exe", "README.txt") 

我的問題是,每次我點擊按鈕,它會打開我的文檔。它必須打開APPpath +特定文件夾。

編輯:

Public Class Form1 



Private Sub ExcisionButtonDefault5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault5.Click 
    Me.Close() 
End Sub 

Private Sub ExcisionButtonDefault1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault1.Click 
    Dim path As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath) & "\mvram.biz\" 
    Process.Start("explorer.exe", path) 
End Sub 

Private Sub ExcisionButtonDefault2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault2.Click 
    Process.Start("explorer.exe", Application.StartupPath & "\Documents") 
End Sub 

Private Sub ExcisionButtonDefault3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault3.Click 
    Process.Start("Notepad.Exe", "/select," & "README.txt") 
End Sub 

Private Sub ExcisionButtonDefault4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault4.Click 
    Process.Start("explorer.exe", System.Windows.Forms.Application.StartupPath + "\Presentation") 
End Sub 

Private Sub ExcisionButtonDefault6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcisionButtonDefault6.Click 
    System.Diagnostics.Process.Start("http://www.mvram.biz") 
End Sub 
End Class 
+0

當你說「每次我點擊按鈕」你的意思是'Button1'? – davidsbro

+0

是先生按鈕1 按鈕2是我試圖打開Readme.txt,但文件沒有找到 請幫助我先生目前堅持這一 – blademaster

+0

看看這個:[您的需求的例子] [ 1] [1]:http://stackoverflow.com/questions/6532222/how-do-i-open-a-windows-explorer-window-with-a-specific-folder-selected – Brainarts

回答

0

爲什麼它會打開一個隨機位置的原因是因爲你正打算執行錯誤的道路(整個應用程序路徑+其他應用程序)。你必須選擇目錄。試試這個代碼:

Dim path As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath) & "\mvram.biz" 
Process.Start("explorer.exe", path) 

其他選項:

Dim path As String = Environment.CurrentDirectory & "\mvram.biz" 

我個人更喜歡使用絕對路徑,而不是相對的人(只是文件的名稱指的是同一個目錄的時候)。

+0

先生,我試過但它只是打開我的文檔。它總是這樣我不明白。 – blademaster

+0

@blademaster然後你的exe文件在我的文檔中。在路徑中設置一個斷點並查看所需的值。此代碼應該完美工作。 – varocarbas

+0

@blademaster,因爲這是你想要的,不是嗎?打開一個名爲「mvram.biz」的文件,它位於與可執行文件相同的文件夾中。 – varocarbas

0

要打開FileExplorer特定的文件夾,你可以嘗試通過這樣的說法:

Dim x as string = "FolderPath" 
Process.Start("explorer.exe", "/root,x") 

或者你也可以傳遞參數"/select,x",這將打開資源管理器與文件夾選中,但未打開。這article可能會有所幫助。或者您可以只擁有文件地址,就像您上面嘗試的那樣,並且它會打開到正確的位置。

然後打開一個txt文件,所有你需要做的是:

Process.Start("FileAddress") 

這將打開其默認編輯器中的文件。

HTH

+0

它只是在我的文件打開先生。我不明白它的目錄必須是 C:\ Users \ tales \ Documents \ Visual Studio 2008 \ Projects \ WindowsApplication1 \ WindowsApplication1 \ bin \ debug – blademaster

+0

@blademaster如果你把C:\ Users \ tales \ Documents \ Visual Studio 2008 \ Projects \ WindowsApplication1 \ WindowsApplication1 \ bin \ debug \「與\最後,它仍然打開您的文檔文件夾? – davidsbro

相關問題