简体   繁体   English

VB.NET shell命令抛出一个未找到异常的文件

[英]VB.NET shell command throwing a file not found exception

I am trying to convert one of my unix text files to a dos text file. 我试图将我的一个unix文本文件转换为dos文本文件。 I am using the following command: 我使用以下命令:

Shell(string.format("unix2dos {0}", sFileCompletePath))

I've already added the unix2dos command in my environment path on the server. 我已经在服务器上的环境路径中添加了unix2dos命令。

But when I execute the above mentioned command I get a FileNotFound exception even when the file is located on the disk. 但是当我执行上面提到的命令时,即使文件位于磁盘上,我也会收到FileNotFound异常。

Is there anything I am missing out? 我有什么遗漏的吗?

I would recommend doing it this way: 我建议这样做:

Public Sub ShellandWait(ByVal ProcessPath As String, ByVal Arguments As String)
        Dim objProcess As System.Diagnostics.Process
        Try
            objProcess = New System.Diagnostics.Process()
            objProcess.StartInfo.Arguments = Arguments
            objProcess.StartInfo.FileName = ProcessPath
            objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
            objProcess.Start()
            'Wait until it's finished
            objProcess.WaitForExit()
            'Exitcode as String
            Console.WriteLine(objProcess.ExitCode.ToString())
            objProcess.Close()
        Catch ex As Exception
            Console.WriteLine("Could not start process " & ProcessPath & "  " & ex.Message.ToString)
        End Try

    End Sub

It's more complicated but gives you more power over your processes. 它更复杂,但可以为您的流程提供更多功能。

If sFileCompletePath contains spaces, it could solve it by adding double quotes around it: 如果sFileCompletePath包含空格,它可以通过在其周围添加双引号来解决它:

Shell(String.Format("unix2dos ""{0}""", sFileCompletePath))

If you want to have more control over the process, it might be better to use the example Chris posted. 如果您想要更好地控制流程,最好使用Chris发布的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM