VB.NET get path from the file the user opened - vb.net

I am developing a media player based on WMP.
If the user sets my media player as the default player to play .avi files for example, then clicks a random .avi file, my player will start successfully, however it will not load the file that the user opened.
So thats why I need a code that will get the path of the file the user opened, so that I can make the player load that file automatically after it has been started.

Use the My.Application.CommandLineArgs property. This gets you a list of commandline arguments. Launching a file from the explorer usually provides the path to the clicked file as the only argument, so use it like:
If My.Application.CommandLineArgs IsNot Nothing AndAlso _
My.Application.CommandLineArgs.Count > 0 Then
Dim UserFile As String = My.Application.CommandLineArgs(0)
PlayFile(UserFile)
End If

Related

ClickOnce: How to Get Double Clicked File Path for Single Instance Program

I have a single instance ClickOnce program with a file association. When the user double clicks a file, I want to load the file. This works great the first time around with this code:
Dim path as string
If AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData IsNot Nothing Then
For Each s As String In AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData
path = New Uri(s).LocalPath
f.loadConfig(path) 'f.loadConfig() is the routine in my code to load the file
Next
End If
The problem is if the program is already running and the user double clicks a 2nd file. When I handle StartupNextInstance and check ActivationData the 2nd time around, it still points to the first file!
In both instances the commandline args appear empty (I've checked).
Note, this is for a ClickOnce Application, which I think use ActivationData to pass the double clicked file's path, at least for the first instance.

Using Visual basic 2017 to navigate to a esp8266 wifi switch (Sonoff)

I am using the below code to navigate to a specific web address as follows :
WebBrowser1.Navigate("http://192.168.0.157/cm?cmnd=POWER%20Toggle"
The fact is that the the link returns a .json file and the WebBrowser controls displays the default save file dialog asking if i want to save the file or run it.
I want to ignore it the dialog and read from the .json file directly(i mean after downloading it).
I just want to get rid of the Save dialog of the webbrowser.I am a newbie so i don't know what to search or how to ask properly.
Though your post is not even close to be standard and hardly explains the issue, what i understand so far is that you have a few issues and i will answer them separately.
Disabling the download dialog of the webbrowser and downloading the files automatically
Firstly, you mentioned it returns a .json file. So , you can easily add a SaveFileDialogto your form or set a custom path(maybe in a variable) and check if the webbrowser is trying to download any .json files. Then you will Cancel the call(typically i mean that cancel the popup that says Save , Run ...) and make use of the SaveFileDialog or the local variable to save the file directly to disk. Here's a sample which uses a local string variable as the path and saves the .json file directly to disk :
Imports System.ComponentModel
...
Dim filepath As String '''class lever variable
Private Sub myBroswer_Navigating(sender as Object, e As WebBrowserNavigatingEventArgs) Handles myBroswer.Navigating
If e.Url.Segments(e.Url.Segments.Length - 1).EndsWith(".json") Then
e.Cancel = True
filepath = "C:\test\" + e.Url.Segments(e.Url.Segments.Length - 1)
Dim client As WebClient = New WebClient()
AddHandler client.DownloadFileCompleted , AddressOf New AsyncCompletedEventHandler(DisplayJson);
client.DownloadFileAsync(e.Url, filepath)
End If
End Sub
Displaying the result AKA .json
It is very easy to de-serialize/parse .json files.But first, download this , extract the ZIP and Add Reference to Newtonsoft.Json.dll. Now consider the bellow code snippet :
Private Sub DisplayJson()
Dim parseJson = Newtonsoft.Json.Linq.JObject.Parse(File.ReadAllLines(filepath))
MsgBox(parseJson("element name here").ToString)
End sub
Hope this helps

Unable to open network PDF file from embedded link inside another PDF

I use a small program to open a main PDF file used by multiple employees. If open the given file on the Network (without using the program) and select the hyperlink inside the PDF, it will open correctly.
However, when I run my program to open that given file and try to select the hyperlink inside the PDF, I receive a "Security Block" error:
The control I am using to display the PDF on the program is:
And I use the following code to load the PDF file:
Private Sub UpdateFile()
Dim fileList As IEnumerable(Of String) = Directory.GetFiles(DrawingsRootDirectory, "*.PDF", SearchOption.TopDirectoryOnly)
Me.AxAcroPDF1.LoadFile(fileList.Last)
Me.AxAcroPDF1.Refresh()
Me.WindowState = FormWindowState.Maximized
End Sub
I'm not quite sure why opening the PDF file via a program would prevent it from using the hyperlinks inside that given file. Any ideas?

No application is associated with the specified file for this operation (VB.NET)

We have a Win Forms application that produces a pdf with iTextSharp, saves it to a local directory and the application then opens the file. With one customer (all XP boxes and Adobe Reader 11) it throws the following error
No application is associated with the specified file for this operation
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
Which would suggest Adobe Reader is not associated correctly with the pdf extension apart from the fact they can navigate to the local directory and open the file without any problem whatsoever.
Anyone come across this oddity before?
Edit re ZippyV - example of a typical sub
Public Sub PDF_Functions_LogCalls_RunReport(ByVal Customer As Boolean)
Try
Dim vOutput As String = LogCalls_Run(Customer)
If Left(vOutput, 5) = "Error" Then
TaskDialog.Show(MainForm, AppBoxError("File Error", vOutput, "Error"))
Exit Sub
End If
If System.IO.File.Exists(vOutput) Then
Dim P As New Process
P.StartInfo.FileName = vOutput
P.StartInfo.Verb = "Open"
P.Start()
End If
Catch ex As Exception
EmailError(ex)
Exit Sub
End Try
End Sub
You're reading the error message wrong. I've added emphasis to the relevant part:
No application is associated with the specified file for this operation
This means that there is no application associated with the verb "Open". Change your code to simply use an empty string (or just don't set) the Verb:
P.StartInfo.FileName = vOutput
P.StartInfo.Verb = ""
P.Start()
This uses whatever the default operation is for the .pdf format, which will match the operation the user would get if they double-clicked the file in Windows Explorer.
Recent versions of Acrobat are setting the default action to "Open with Adobe Reader XI" instead of just "Open", as you can see if you right-click a .pdf file.
This is seemingly what's causing the "not associated for this operation" error.
This error actually happens when there is a difference between the default behaviour of opening the file and the relative behaviour of opening the file.
For example, if you have selected the default application to open .pdf files as Internet Explorer, and you are trying to open the same file using Process.Start() method. You will receive an exception because as per the default operations it should open that file in Internet Explorer and your application is trying to open it using Adobe reader.
To rectify this set the default application for the .pdf file as Adobe Reader and you won't receive this error any more.

File Extension of opened file in VB.NET

I have a small requirement and that is as follows:
I have opened a file using the "openFileDialog" provision and i have set the filer as (*.txt). Now when the openFileDialog is displayed, only the test files are visible, but the user can also select "All Files" and choose any other file type.
Now what is require is that, if a file type other than .txt is selected by the user, i want to display an error message.
So is there any provision by which i can get to know the file type that is selected by the user.
Regards,
George
Look at http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx
Dim fileName As String = "C:\mydir.old\myfile.ext"
Dim extension As String
extension = Path.GetExtension(fileName)
Console.WriteLine("GetExtension('{0}') returns '{1}'", fileName, extension)
You can use the FileOK event to show the message box while the dialog is still open. Use the GetExtension method to determine the extension.
You should also look at the Filter property of the dialog. If you set it correctly "All Files" should not be shown anymore.
Example:
dlg.Filter = "Test-Files (*.txt)|*.txt"