通过我的表单,我打开一个.exe,它位于debug中的一个文件夹中,所以:
\ bin \ Debug \ folder \ .exe
.exe打开并创建3个不同的文件然后关闭。它意味着在与.exe相同的文件夹中创建它们,但是当使用Process.Start()通过VB.net打开时,它会在Debug文件夹中创建它。
是否有人可以解决此问题,因此我不必移动文件?
修改 (.exe本身会创建文件,有些东西可能会在此子元素之外声明)
Private Sub btnRunExe_Click(sender As Object, e As EventArgs) Handles btnRunExe.Click
If AcptEULA.Checked = True Then
Localpath = Application.StartupPath() + "\MCserver" + "\minecraft_server." + txtVersion.Text + ".exe"
Downloadpath = "https://s3.amazonaws.com/Minecraft.Download/versions/" + txtVersion.Text + "/minecraft_server." + txtVersion.Text + ".exe"
LocalpathParent = Application.StartupPath() + "\MCserver"
Try
Dim dirs As String() = Directory.GetFiles(LocalpathParent, "minecraft_server*.exe")
Dim dir As String
For Each dir In dirs
Process.Start(dir)
Next
Catch
'Console.WriteLine("The process failed: {0}", e.ToString())
End Try
ElseIf AcptEULA.Checked = False Then
MsgBox("You must accept the Minecraft EULA before continuing")
End If
End Sub
答案 0 :(得分:1)
Process.Start可以与ProcessStartInfo实例一起使用,您可以在其中设置WorkingDirectory属性。
Dim psi As ProcessStartInfo = New ProcessStartInfo()
psi.WorkingDirectory = LocalpathParent
For Each fileName In Directory.EnumerateFiles(LocalpathParent, "minecraft_server*.exe")
psi.FileName = fileName
Process.Start(psi)
Next
请注意,我已经使用Directory.EnumerateFiles更改了Directory.GetFiles,它允许您在循环文件夹文件时处理文件,而无需在数组内的内存中加载所有文件名。如果您仍想使用GetFiles,那么
Dim psi As ProcessStartInfo = New ProcessStartInfo()
psi.WorkingDirectory = LocalpathParent
Dim files as String() = Directory.GetFiles(LocalpathParent, "minecraft_server*.exe")
For Each fileName In files
psi.FileName = fileName
Process.Start(psi)
Next
如果您查看MSDN上的ProcessStartInfo documentation,您可以找到许多其他有用的属性来微调程序的执行方式。