【问题标题】:Setting Path dir name with spaces for Process为进程设置带有空格的路径目录名称
【发布时间】:2019-02-07 18:44:19
【问题描述】:

我正在尝试通过 Process 对象从 VB.NET 代码打开 Outlook。

Dim PSI As New ProcessStartInfo

' ORG - 
'PSI.FileName =  GetOutlookPath() & My.Settings.OutlookAppExe ' Returns C:\Program Files\Microsoft Office\Office16\Outlook.exe
'PSI.Arguments = My.Settings.OutlookAppArgs
 
PSI.FileName = IO.Path.Combine(GetOutlookPath(), My.Settings.OutlookAppExe)  ' "/e ""C:\Program Files\Microsoft Office\Office16\Outlook.exe""" 

'PSI.Arguments = “/importprf \\comp.us\syscol\comp.us\Software\Outlook2010\comp.prf” ' My.Settings.OutlookAppArgs
 

这里是 GetOutlookPath() 方法:

Private Function GetOutlookPath() As String
    Dim Val As String = ""
    If My.Settings.OutlookAppPath = "" Then
        Try
            Dim Key As RegistryKey = Registry.LocalMachine.OpenSubKey(My.Settings.OutlookRegPath)
            Val = Key.GetValue("Path")
            Val = Val.Replace(";", "")
            If Val.LastIndexOf("\") <> Val.Length - 1 Then
                Val = Val & "\"
            End If
        Catch ex As Exception
            Log.Add("Unable to get registry value for Outlook: " & ex.Message)
            Throw New Exception("Unable to get registry value for Outlook: " & ex.Message)
        End Try
    Else
        Val = My.Settings.OutlookAppPath
    End If
    Log.Add("GetOutlookPath: " & Val)
    Return Val
End Function

以上代码在 Win 7 上运行,但在某些 Win 10 系统上会产生问题。我只是抛出 dir name not found 异常。我尝试将设置变量的值替换为实际内容以尝试,以及上述所有使用带空格的路径的方法,但没有任何效果。谁能帮助我知道如何设置路径值并使其在所有 Win OS 上工作。在 Process 中传递的参数也很难。

打开 IE 也会出现同样的错误。这是IE的代码:

Private Sub LaunchIE(ByVal UserName As String, ByVal SecurePassword As SecureString, ByVal Domain As String, Optional ByVal WebSite As String = "http://iserver1/comp")
    Try
        Log.Add("LaunchIE: UserName " & UserName & " SecurePass Length: " & SecurePassword.Length & " Domain: " & Domain & " WebSite: " & WebSite)
        Dim PSI As New ProcessStartInfo
        'PSI.FileName = GetIEPath() & My.Settings.IEAppExe
        'PSI.Arguments = WebSite
      '' Tried to open notepad - no space in path, yet it throws "The directory name is invalid" exception

        PSI.FileName = IO.Path.GetFullPath("C" & IO.Path.VolumeSeparatorChar & "\Windows\notepad.exe") ' Value = C:\Windows\notepad.exe

        PSI.UserName = UserName
        PSI.Password = SecurePassword
        PSI.Domain = Domain
        PSI.LoadUserProfile = True
        PSI.UseShellExecute = False

        IEProc.StartInfo = PSI
        IEProc.Start()
    Catch ex As Exception
        Log.Add("LaunchIE Failed: " & ex.Message)
        Throw New Exception("Unable to launch Internet Explorer: " & ex.Message)
    End Try
End Sub

你能帮我弄清楚我该如何处理这种情况吗?

谢谢

【问题讨论】:

  • My.Settings.OutlookRegPath 的值是多少?
  • OutlookRegPath = SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE
  • 是 32 位还是 64 位的注册表问题? kunal-chowdhury.com/2016/06/how-to-retrieve-office-version.html
  • 您能否提供更多关于您看到将参数传递到 ProcessStartInfo 的“困难时期”的具体信息? IE 提供例外情况
  • 在 2 Win 10 IE 上也抛出同样的错误(有 Office 16)。在 1 Win 10 上,Outlook 和 IE 都打开(有 Office 15)。我检查了 Wow6432 和 32 位的注册表项的“路径”值,两者具有相同的值,即非工作系统上的“C:\Program Files\Microsoft Office\Office16”。

标签: vb.net process directory spaces


【解决方案1】:

设置 ProcessStartInfo 的 'WorkingDirectory' 属性,否则您将得到“目录名称无效”。

根据:https://stackoverflow.com/a/25072978/373334

【讨论】:

  • 感谢 Jake1164,这确实完全解决了我的问题。我将我的工作目录添加为 PSI.WorkingDirectory = IO.Path.GetPathRoot(GetOutlookPath()) ' 这给出了路径的根驱动器。使用directoryName,目录中的空间再次导致问题。所以将根驱动器设置为工作目录,它就可以工作了。再次感谢您。
猜你喜欢
  • 2010-10-10
  • 2013-08-21
  • 2019-04-02
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多