vb.net opening custom file extension with clickonce application - vb.net

My program has two different major functions.
By Default - opening the program itself with the .appref-ms (desktop shortcut) opens the program in the normal mode.
I have also created a .TRA extension. When a .TRA file is opened with my program it should open the program in a secondary mode.
I can see that my program has been registered with the .tra extension and the icons have all been changed.
If I drag a file with the .TRA extension onto the .exe file in the debugging bin it opens as-expected in this second mode. However, by simply double-clicking the .TRA file it is still opening in the default mode.
The code is set to look at all of the command lines passed for the .TRA extension:
For i = 0 To System.Environment.GetCommandLineArgs.Length
Dim _file = System.Environment.GetCommandLineArgs(i)
Dim format As String = IO.Path.GetExtension(_file)
If format = ".TRA" Then
file = System.Environment.GetCommandLineArgs(i)
openedwithext = True
Exit For
End If
Next i
What am I missing?
Is double-clicking my .TRA file not sending a command-line argument to my app as I'm thinking it does?
How else could this be performed?
Thanks in advance.

Because the .appref-ms works like a shortcut it takes the commandline arguments differently.
I solved the issued by adding the System.Deployment namespace and using the following code on the .loaded event.
'Are we launching as a click-once?
If (IsNetworkDeployed) Then
Try
Dim urifile As New Uri(AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData(0))
Dim uristring As String = urifile.ToString
If uristring.Contains(".TRA") Then
file = uristring.Substring(8) 'Substring removes the - file:///
openedwithext = True
End If
Catch ex As Exception
End Try
End If

Related

Error thrown trying launch a webpage from a button

I created a button and a menu item in vb.net (.NET 6). I found several answers here on SO that say the process to launching a webpage from such an event can be launched with this code:
Dim webAddress As String = "http://www.example.com/"
Process.Start(webAddress)
However, trying launch the code, I'm given the error of "system cannot find the file specified".
Looking more into it, I know that .NET 6 is running a bit differently and changed the code to the following:
Using link As New Process()
link.StartInfo.UseShellExecute = True
link.Start(New ProcessStartInfo("https://example.com"))
End Using
But still to no avail, and I am given the same error. "System cannot find the file specified." I can run addresses via the regular Windows Run prompt... but the program still cannot launch.
Following Jimi's comment to my original question, I changed the Sub to the following:
Sub LaunchWebsite(strWebpageURL As String)
Using Process.Start(New ProcessStartInfo(strWebpageURL) With {.UseShellExecute = True})
End Using
End Sub
Using this, the webpage launched in my desktop's default browser with no problem.
You can use
Respone.Redirect("http://www.example.com/")
or use javascript in server side code
Dim url As String = "http://www.example.com"
Dim s As String = "window.open('" & url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');"
ClientScript.RegisterStartupScript(Me.GetType(), "script", s, True)
Above code open webpage in new popup window.
Regards
Aravind

Writing to Command Line Not Working

An application I need to use (USB capture utility) has a .cmd version that I can call from my Visual Basic code. I am able to launch the application and put it in "command line mode" like this:
Public Class MyClass
Dim StreamWriteUtility As System.IO.StreamWriter
Dim StreamReadUtility As System.IO.StringReader
Dim ProcessInfo As ProcessStartInfo
Dim Process As Process
Public Sub StartUSBCapture(ByVal DataStorageLocation As String)
Dim ProcessInfo As ProcessStartInfo
Dim Process As New Process
ProcessInfo = New ProcessStartInfo("C:\FW_Qualification_Suite\data-center-windows\data-center\bin\datacenter.cmd", "-c ")
ProcessInfo.CreateNoWindow = True
ProcessInfo.UseShellExecute = False 'Must be changed if redirect set to True
ProcessInfo.RedirectStandardInput = True
Process = Process.Start(ProcessInfo)
SWUtility = Process.StandardInput
While True
SWUtility.WriteLine("run") 'Looping for test to ensure this isn't a timing issue
End While
End Sub
End Class
This launches the application and opens a separate command line window that should accept further commands (i.e., capture, run, stop, etc). However, I am having trouble getting those subsequent commands to show up in the command line window. I've tried redirecting the standard input of the process, but still nothing shows up in the console window.
Can anyone tell how I'm supposed to actually get these commands from my Visual Basic program into this application?

Printing a .ZPL file to a zebra printer in vb.net. Visual Studio 2015 version

I already have the raw ZPL file ready to go, I just don't know how to set the printer I want to send it to and then send it. How would I do this?
Note: I have a batch script on my computer that I default all ZPL files to, which is a shell script that sends the file to the thermal printer on my computer. I want to get away from that and have all the commands within my application so I don't have to use an external script like that.
This is the code I have now that when ran it auto opens with my batch script:
Sub SaveLabel(ByRef labelFileName As String, ByRef labelBuffer() As Byte)
' Save label buffer to file
Dim myPrinter As New PrinterSettings
Dim LabelFile As FileStream = New FileStream(labelFileName, FileMode.Create)
LabelFile.Write(labelBuffer, 0, labelBuffer.Length)
LabelFile.Close()
' Display label
DisplayLabel(labelFileName)
End Sub
Sub DisplayLabel(ByRef labelFileName As String)
Dim info As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo(labelFileName)
info.UseShellExecute = True
info.Verb = "open"
info.WindowStyle = ProcessWindowStyle.Hidden
info.CreateNoWindow = True
System.Diagnostics.Process.Start(info)
End Sub
And this is my batch script:
copy %1 \\%ComputerName%\Zebra
To replicate the exact functionality of the batch file in VB.net:
Dim filename As String = System.IO.Path.GetFileName(labelFileName)
System.IO.File.Copy(
labelFileName,
Environment.ExpandEnvironmentVariables("\\%ComputerName%\Zebra\" & filename))
This copies the file using the method provided by the System.IO namespace. It also expands the %COMPUTERNAME% environment variable. This replaces all the code in the DisplayFile subroutine.

Identifying if a JPG file is open

I've been trying to set an error trap that will detect if a file is already open. This is no problem when the file is a text file using the following code:
Private Function FILEOPEN(ByVal sFile As String) As Boolean
Dim THISFILEOPEN As Boolean = False
Try
Using f As New IO.FileStream(sFile, IO.FileMode.Open)
THISFILEOPEN = False
End Using
Catch
THISFILEOPEN = True
End Try
Return THISFILEOPEN
End Function
My problem is that when the file is an open JPG file, not a text file, the above function returns False indicating that it is not open? I have tried different variations of the function but still cannot find a function that can tell if a JPG file is open.
You should NOT do this kind of behavior. Simple answer is because after you check, but before you do anything with it, the file may become unavailable. A proper way is to handle an exception as you access the file. You may find this answer helpful:
https://stackoverflow.com/a/11288781/897326

how to open selected files through the default application of the file in VB 2010?

I have Windows application written in VB 2010.Here, user may select any file from open dialog.So, I want to open the file in corresponding application.for example, suppose user selecting docx file then i have to open the file using msword,suppose,if it is a pdf file, then i have to open with adobe reader or available pdf reader(default application).
Is this possible to do?
Shell and the Windows API CreateProcess() are for starting executable files.
If you're loading a document/file then these are handled by ShellExecute() and can be initiated in .NET using the Process.UseShellExecute property:
Private Function ShellExecute(ByVal File As String) As Boolean
Dim myProcess As New Process
myProcess.StartInfo.FileName = File
myProcess.StartInfo.UseShellExecute = True
myProcess.StartInfo.RedirectStandardOutput = False
myProcess.Start()
myProcess.Dispose()
End Function
Taken from the #VB wiki.
Try this:
now with openfiledialog
Dim OpenFileDlg as new OpenFileDialog.
OpenFileDlg.FileName = "" ' Default file name
OpenFileDlg.DefaultExt = ".xlsx" ' Default file extension
OpenFileDlg.Filter = "Excel Documents (*.XLSX)|*.XLSX"
OpenFileDlg.Multiselect = True
OpenFileDlg.RestoreDirectory = True
' Show open file dialog box
Dim result? As Boolean = OpenFileDlg.ShowDialog()
' Process open file dialog box results
for each path in OpenFileDlg.Filenames
Try
System.Diagnostics.Process.Start(Path)
Catch ex As Exception
MsgBox("Unable to load the file. Maybe it was deleted?")
End Try
If result = True Then
' Open document
Else
Exit Sub
End If
next
This will work if the file is registered with the OS. Use Try catch because it can throw errors if the file is in use.
Edit: It uses always the default application.