在选定的路径中运行脚本

发布于 2024-11-30 21:54:58 字数 1363 浏览 1 评论 0原文

我目前有这段代码:

   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

I currently have this piece of code:

   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

This copies two files in the current working directory (which will be the default install folder) to the selected path from the Fodler Dialog Browser. This works correctly.

Now what I want to do is too run "pdfmerge.py" into the selected folder path. I tried the below code but the script is still running in the current working directory.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌若浮生 2024-12-07 21:54:58

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

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

一个问题:您确保 dlgFolder.SelectedPath 正确吗?在不知道程序内部工作原理的情况下,似乎可以在 Button1 之前按 BtnNow,这意味着 dlgFolder.SelectedPath 不会被设置用户。

You can set the process's working directory.

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

One question: are you ensuring the dlgFolder.SelectedPath is correct? Without knowing the inner workings of your program, it appears possible to press BtnNow before Button1, meaning dlgFolder.SelectedPath won't have been set by the user.

待天淡蓝洁白时 2024-12-07 21:54:58

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

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

您可以为 userNamepassword 传入 null,但如果您的目录不在您有权访问的标准目录范围内,您可能需要输入您的用户名和密码。我认为 domain 将是工作目录。

Try using the overload of Process.Start() that takes 5 arguments.

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

You may be able to pass in null for userName and password, but if your directory is outside of the standard ones that you have permission for, you may need to put your username and password in. domain would be the working directory, I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文