自動翻訳(Auto Translation)
- English
- 日本語
VB.netでURLを開く方法の備忘録
VB.netのソフトで、URLを開くためにProcess.Startを使用した場合、「指定したファイルが見つかりません」といった内容で、URLが開けない場合があります。
VB.NET
Dim strURL As String = String.Empty
strURL = "http://www.youtube.com/watch?v=phjIw8h31J0"
Process.Start(strURL)
例外発生時にプラットフォームに応じて、開くアプリケーションを指定することで解決できます。
VB.NET
Dim strURL As String = String.Empty
strURL = "http://www.youtube.com/watch?v=phjIw8h31J0"
Try
Process.Start(strURL)
Catch ex As Exception
If RuntimeInformation.IsOSPlatform(OSPlatform.Windows) Then
'Windowsの場合
strURL = strURL.Replace("&", "^&")
Process.Start(New ProcessStartInfo("cmd", $"/c start {strURL}"))
ElseIf RuntimeInformation.IsOSPlatform(OSPlatform.Linux) Then
'Linuxの場合
Process.Start("xdg-open", strURL)
ElseIf RuntimeInformation.IsOSPlatform(OSPlatform.OSX) Then
'OSX(Mac)の場合
Process.Start("open", strURL)
Else
Throw
End If
End Try