在所选路径中运行脚本

时间:2011-08-22 10:36:54

标签: vb.net process cmd directory

我目前有这段代码:

   Sub Button1Click(sender As Object, e As EventArgs)



            If dlgFolder.ShowDialog = Windows.Forms.DialogResult.OK Then

                txtPath.Text = dlgFolder.SelectedPath


                Try

                    Dim CopyFile As String = Path.Combine(Directory.GetCurrentDirectory, "pdftk.exe")
                    Dim CopyLocation As String = Path.Combine(dlgFolder.SelectedPath, "pdftk.exe")
                    Dim pyScript As String = Path.Combine(Directory.GetCurrentDirectory, "pdfmerge.py")
                    Dim pyLocation As String = Path.Combine(dlgFolder.SelectedPath, "pdfmerge.py")

                    System.IO.File.Copy(CopyFile, CopyLocation, True)
                    System.IO.File.Copy(pyScript, pyLocation, True)

                Catch copyError As IOException
                Console.WriteLine(copyError.Message)
                End Try         
            End If
End Sub

将当前工作目录中的两个文件(将是默认安装文件夹)从Fodler对话框浏览器复制到所选路径。这可以正常工作。

现在我想要做的是将“pdfmerge.py”运行到选定的文件夹路径中。我尝试了以下代码,但脚本仍在当前工作目录中运行。

Sub BtnNowClick(sender As Object, e As EventArgs)

        Dim myProcess As Process
        Dim processFile As String = Path.Combine(dlgFolder.SelectedPath, "pdfmerge.py")

        myProcess.Start(processFile, dlgFolder.SelectedPath)



    End Sub

2 个答案:

答案 0 :(得分:1)

您可以设置流程的工作目录。

Dim p As New ProcessStartInfo
p.FileName = Path.Combine(dlgFolder.SelectedPath, "pdfmerge.py")
p.WorkingDirectory = dlgFolder.SelectedPath
Process.Start(p)

一个问题:您确定dlgFolder.SelectedPath是否正确?如果不了解您的计划的内部运作情况,可能会在BtnNow之前按Button1,这意味着用户不会设置dlgFolder.SelectedPath

答案 1 :(得分:0)

尝试使用带有5个参数的Process.Start()重载。

Start ( _
fileName As String, _
arguments As String, _
userName As String, _
password As SecureString, _
domain As String _
)

您可以为nulluserName传递password,但如果您的目录不在您获得许可的标准目录之外,则可能需要将我认为。domain中的用户名和密码将是工作目录。