为进程设置带空格的路径目录名称
Setting Path dir name with spaces for 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
你能帮我弄清楚我该如何处理这种情况吗?
谢谢
设置 ProcessStartInfo 的 'WorkingDirectory' 属性 否则您将获得 "The directory name is invalid".
根据:
我正在尝试通过进程对象从 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
你能帮我弄清楚我该如何处理这种情况吗?
谢谢
设置 ProcessStartInfo 的 'WorkingDirectory' 属性 否则您将获得 "The directory name is invalid".
根据: