vb.net 打开应用程序路径+特定文件夹的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19303355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Button that opens Application Path + specific folder
提问by blademaster
I'm kind of confused on how to do this. What I want to do is when I click Button1, my program will open a folder in Explorer, and the second button will open a file as a text file.
我对如何做到这一点感到有些困惑。我想要做的是当我点击 时Button1,我的程序将在资源管理器中打开一个文件夹,第二个按钮将打开一个文件作为文本文件。
Here's my code:
这是我的代码:
Button 1
按钮 1
Process.Start("explorer.exe", Application.ExecutablePath + "\mvram.biz")
Button 2
按钮 2
Process.Start("Notepad.Exe", "README.txt")
My problem is everytime I click the button it will open My Documents. It must open the APPpath+Specific Folder.
我的问题是每次单击按钮时它都会打开我的文档。它必须打开 APPpath+特定文件夹。
EDIT:
编辑:
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
采纳答案by varocarbas
The reason why it opens a random location is because you are intending to execute a wrong path (whole app path + other app). You have to choose the directory. Try this code:
它打开随机位置的原因是因为您打算执行错误的路径(整个应用程序路径+其他应用程序)。您必须选择目录。试试这个代码:
Dim path As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath) & "\mvram.biz"
Process.Start("explorer.exe", path)
Other option:
其他选择:
Dim path As String = Environment.CurrentDirectory & "\mvram.biz"
I personally prefer to use absolute paths rather than relative ones (just the name of the file when referring to the same directory).
我个人更喜欢使用绝对路径而不是相对路径(在引用同一目录时仅使用文件名)。
回答by davidsbro
To open a specific folder in FileExplorer, you can try passing this argument:
要在 FileExplorer 中打开特定文件夹,您可以尝试传递此参数:
Dim x as string = "FolderPath"
Process.Start("explorer.exe", "/root,x")
Or you could pass the argument "/select,x", which will open File Explorer with the folder selected, but not opened. This articlemay be helpful. Or you can just have the file address, like you tried above, and it will open to the correct location.
或者您可以传递参数"/select,x",这将打开文件资源管理器并选择文件夹,但未打开。这篇文章可能会有所帮助。或者您可以只拥有文件地址,就像您在上面尝试的那样,它将打开到正确的位置。
And then to open a txt file, all you need to do is:
然后打开一个txt文件,你需要做的就是:
Process.Start("FileAddress")
This will open the file in its default editor.
这将在其默认编辑器中打开文件。
HTH
HTH

