我正在尝试通过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系统上会出现问题。我只是抛出目录名称未找到异常。我尝试用实际内容替换Settings变量的值,并尝试使用以上所有方法处理带空格的路径,但没有任何效果。谁能帮我知道如何设置路径值并使它在所有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
能否请您帮我弄清楚如何处理这种情况。
谢谢
答案 0 :(得分:1)
设置ProcessStartInfo的'WorkingDirectory'属性,否则将获得“目录名称无效”。