我需要帮助我开始用VB.NET编写的程序。
该程序需要在Chrome中打开网页。在我们这里的20台个人电脑上,其中一些安装了Chrome,其中一些安装了新的Chrome。我不知道新的Chrome是什么,但它似乎存在。
我希望程序扫描计算机以安装Chrome。如果找不到Chrome,则该程序现在应该搜索新的Chrome。如果找不到它们,则会显示一个对话框,提醒用户Chrome未正确安装。
到目前为止我的代码如下:
Try
If voiptools.Checked = True Then Process.Start("C:\Program Files\Google\Chrome\Application\chrome.exe", "--new-window https://toolshd.dcn.ote.gr/voiptools/")
Catch
If voiptools.Checked = True Then Process.Start("C:\Program Files\Google\Chrome\Application\new_chrome.exe", "--new-window https://toolshd.dcn.ote.gr/voiptools/")
End Try
到目前为止一直很好,但是当找不到Chrome或新的Chrome时,我无法弄清楚如何显示消息框。
我要感谢你的耐心,因为我刚刚开始。
许多人祝愿新的一年。
答案 0 :(得分:2)
不要驱动程序试图处理异常。特别是如果有工具可以帮助您避免最常见的异常情况。在您的情况下,我将使用File.Exists(Imports System.IO)来检查文件是否存在。
Dim chrome As String = "C:\Program Files\Google\Chrome\Application\chrome.exe"
Dim newchrome As String = "C:\Program Files\Google\Chrome\Application\new_chrome.exe"
Dim prms = "--new-window https://toolshd.dcn.ote.gr/voiptools/"
Try
If voiptools.Checked = True Then
If File.Exists(chrome) Then
Process.Start(chrome, prms)
Else If File.Exists(newchrome) Then
Process.Start(newchrome, prms)
Else
' write here your message for your users ' something like alert("No suitable file to launch found")
End If
Catch ex as Exception
' write here your handler for exceptional situations (log, message)
End Try
Sub alert(msg as string)
string script = "alert(" & msg & ");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
End Sub
请注意,您仍然必须使用Try Catch块,因为每次使用文件系统时,您都有可能在没有控件(权限,文件被用户删除等等)的特殊情况下发生。