Open multiple files using arguments - vb.net

I'm using this code to load multiple files using windows context menu, but the problem is that the aplication is open many times as files the user has selected.
For example: If I select 14 files, an open them with the application, the aplicacion is opened 14 times and load the form only one.
But there is a way to send all arguments once? Because %1 send only one file, or there is for example a %2 that send all file pats in one argument? If there is I'vent found.
This my actual code:
Public Class Program
Public Shared Sub Main()
Dim FurBase As New Core.clsDatabase
FurBase.Directory = My.Application.Info.DirectoryPath
Dim returnValue As String()
returnValue = Environment.GetCommandLineArgs()
If returnValue.Length > 1 Then
FurBase.AddTemporalFilepath(returnValue(1).ToString)
End If
If Not Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then
ShowUploader()
End If
End Sub
Private Shared Sub ShowUploader()
Dim Uploader As New frmUploader
Application.EnableVisualStyles()
Application.Run(Uploader)
End Sub
End Class
Please tell me what think about the code and if ther is any way to improve it.
Regards~

I was reading about that today; seems you'll need to deal with a DDE server.
There are an old question which can help you: What is the best .net alternative to dde for file associations?

Related

SolidWorks API, Macro working in VSTA but not from dll

Really appreciate who can spend couple of minutes to help me out, so thanks in advance !
Got myself into situation where running macro in VSTA works (vb.net), but running dll files from solid works does not work. Probably forgetting something very simple. Principle is that text file is in same folder as dll files and by default read from that folder without long location "string"
This works in VSTA and after building dll (very simple)
Partial Class SolidWorksMacro
Public Sub main()
Dim Model As ModelDoc2 = swApp.ActiveDoc
Dim LayerName As String = "Stamp"
MsgBox(LayerName)
End Sub
Public swApp As SldWorks
End Class
No I want to do same thing in a way that layer name is read from text file. It works when running from VSTA, but after building to dll and running from solid works it gives error: cannot open
"Location"\macro.dll.
Partial Class SolidWorksMacro
Public Sub main()
Dim Model As ModelDoc2 = swApp.ActiveDoc
Dim LayerName As String = "Stamp"
Dim FileName As String = "LayerName.txt"
Dim LayerName As String
Dim sr As New StreamReader(FileName)
LayerName = sr.ReadLine
MsgBox(LayerName)
End Sub
Public swApp As SldWorks
End Class
How are you planning on running the code? You will have to build out additional functionality to create a button/taskpane/property page through the API for SOLIDWORKS to know what you want to do. It is a little more complicated than running a macro. What references did you add to your project? You will need to add at least:
SolidWorks.Interop.sldworks
SolidWorks.Interop.swpublished
Are you properly implementing the ISwAddin interface? Also, I have not had much luck merely opening a DLL with SOLIDWORKS, I use regasm.exe to register the COM DLL or create a wix installer to create the registry entries if distributing to multiple machines.
The Getting Started page in the API help will be a good reference to see some examples and how to configure your environment.

read event log detail in vb.net

I'm writing a application at the moment, part of it will be scraping some information from the windows event log, it mostly works....
Dim strValue As String
Dim objLogs() As EventLog
Dim Logname As String = "Application"
Dim objEntry As EventLogEntry
Dim objLogEntry As EventLogEntry
Dim objLog As EventLog
objLogs = EventLog.GetEventLogs()
For Each objLog In objLogs
If objLog.LogDisplayName = Logname Then
For Each objLogEntry In objLog.Entries
WriteLine("EventID")
WriteLine("Machinename")
WriteLine("message")
Next
Exit For
End If
Next
This will happily write out the EventID, machine name and event message.
details tab in event viewer
What I can't figure out is how to output the "details" tab in event viewer ideal into strings or similar.
MSDN isn't being helpful, could anyone point me in the right direction please?
Thanks in advance,
Looks like the details in the EventLogEntry is represented by the Data property that is stored as a byte array. You would have you would have to then converted to something readable. But the format of the data seems to vary on the Windows OS version.
Here an alternative way to do it. Code copied from answer to from the following question and converted to VB.Net.
Serializing a .NET EventLogEntry instance to XML
Imports System.Diagnostics.Eventing.Reader
Sub Main()
Dim query As New EventLogQuery("System", PathType.LogName)
Dim watcher As New EventLogWatcher(query)
AddHandler watcher.EventRecordWritten, AddressOf watcher_EventRecordWritten
watcher.Enabled = True
Console.ReadLine()
End Sub
Public Sub watcher_EventRecordWritten(sender As Object, e As EventRecordWrittenEventArgs)
Console.WriteLine(e.EventRecord.ToXml())
End Sub
Put in the main module of a simple console application and an watches for system events. Writing out the event data converted to XML as one long string.
Worked using 4.5 framework on a Windows 7 machine.

VB.NET batch pdf printer tool

I'm looking for a tool, but I can't find it. So I thought, maybe I can make one. Seems like a good idea, a program that fits my needs at last! Just one problem.. I don't know how!
Intro
At my work we create production drawings in .dwg format. Once this is done they are exported as .pdf sent to the client for approval. If approved they are issued for production, so they need to be plotted.
So my current workflow is:
Set my default printer preferences (number of copies, paper size, ..)
Go to the folder that has the .pdf files
Select 15 files
Right click print.
The problem here is
The files are printed in the order the printer receives them and not the order in the folder. So I would need to sort them out ( this x number times the number of copies sometimes this goes up to 6 times. )
I can only do 15 at a time (large projects are +100 documents)
I have to reset my preferences each time the sizes change.
Our file numbering system has some "intelligence" into it. Its build like this:
A1-12345-001 rev1
A1 = page size
12345 = document number, the same within a project. Other project means other number. (mostly irrelevant as I only print a project at a time)
001 = sequence number, next drawing will be 003,004,005,007,... you get the drift.
rev1 = revision number. Normally only the highest revision should be located in the folder, but it could be used to check for documents with a lower revision.
Now, I would like to automate my happy printing task, as this makes my week very bad if a large projects needs to go into production or, there are revisions mid-production and we need to re-issue them for production.
So far the situation sketch
Program sketch
So I've started to make a sketch of what the program should do.
User should be able to add .pdf files into a list. file browser object for starters, can be drag and drop later (the drawings are in one folder so there is no real need for drag and drop).
The list should contain 4 columns. paper size, document number, sequence, revision. Could be a data-grid view.
The list should be sorted by the drawing sequence. This how I would like to pass the drawing so, no sorting is required anymore! You can look at the sequence as page numbers.
Select the desired printer to do the job. (usually the same but other departments also have a printer so that would be a nice option.)
Set the number of copies. When printed, it shouldn't be 5x -001 then 5x -002 .. it should print them still in order of the sequence number and the re-loop the process x times.
Print the documents.
Adding PDF files
I started out with creating a dialog.. easy enough I guess.(please keep in mind that I'm a very low level programmer and nothing seems easy to me..) I added an open file dialog to my windows-form set multiple select true. Also made a filter to have only .pdf files
added a button with this code:
Private Sub ADD_FILES_Click(sender As Object, e As EventArgs) Handles ADD_FILES.Click
' Show the open file dialog. If user clicks OK, add pdf_document to list(of T)
If FileDialog.ShowDialog() = DialogResult.OK Then
Dim file As String
For Each file In FileDialog.FileNames
Debug.Print(file)
Next
End If
End Sub
So this should give me all the info I need from the file. I will edit this later, I know how to access it now!
The document object
I guess it would be wise to use some o.o.p. for this. As each file is a document and have the same required properties to make this work.
So I made a Public class for the document called PDF_Document
Public Class PDF_Document
Public FullFilePath As String
Public Property Size As String
Public Property DocNumber As String
Public Property Sequence As String
Public Property Revision As String
Public Sub New(ByVal oFilePath As String)
' Set the FullFilePath
FullFilePath = oFilePath
' Get the filename only without path.
Dim oFileName As String
oFileName = Path.GetFileName(oFilePath)
' Get the document size from the file name
Size = oFileName.Substring(0, 2)
' Get the document number from the file name
DocNumber = oFileName.Substring(3, 5)
' Get the sequence from the file name
Sequence = oFileName.Substring(9, 3)
' Chop of the .pdf from the name to get access the revision
Revision = oFileName.Substring(oFileName.Length - 5, 1)
End Sub
End Class
Well this should result into the info I need from the document..
creating a list(of t)
Wow, it seems to be getting somewhere.. Now to hold the list I'll have this collection I think don't know whats best for this? Public oPrintList As New List(Of PDF_Document)
So I think I should populate my list like this?
Public oPrintList As New List(Of PDF_Document)
Private Sub ADD_FILES_Click(sender As Object, e As EventArgs) Handles ADD_FILES.Click
' Show the open file dialog. If user clicks OK, add pdf_document to list(of T)
If FileDialog.ShowDialog() = DialogResult.OK Then
Dim oFile As String
For Each oFile In FileDialog.FileNames
Dim oPDF As New PDF_Document(oFile)
oPrintList.Add(oPDF)
Next
End If
End Sub
Making things visual for the user
Hmm okay, were getting somewhere, I got a list of all the files I need! But I want to see them in a viewer. I'm gonna use a data-grid view maybe? It should show my properties and it looks good I think.
So I made a binding that binds my List(of T) and added this binding as data-source for the data-grid view. I also change the ADD_FILES_Click a little, the PDF_Document is now added into the binding and not in the List(of T).
Public Class Form1
Public oPrintList As New List(Of PDF_Document)
Public oBinding As BindingSource
Private Sub ADD_FILES_Click(sender As Object, e As EventArgs) Handles ADD_FILES.Click
' Show the open file dialog. If user clicks OK, add pdf_document to list(of T)
If FileDialog.ShowDialog() = DialogResult.OK Then
Dim oFile As String
For Each oFile In FileDialog.FileNames
Dim oPDF As New PDF_Document(oFile)
oBinding.Add(oPDF)
Next
End If
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
oBinding = New BindingSource(oPrintList, Nothing)
DataGridView1.DataSource = oBinding
End Sub
End Class
Printing the list
Well I managed to do quite some things now. But.. the main essence still isn't reached! I now got a form with a bunch of buttons that don't work yet, a list of select documents in queue for printing and .. thats it :)
Now I'm trying to create a method that prints a .pdf file.. easier sad than done. I need some help with this.. I search the net, but I can find samples that don't work, or I don't understand. All users PC's are equiped with acrobat reader.
Also feel free to comment me on the other parts of the program. ( yes I know there is no sorting function yet etc. ) but getting a page from the printer is more important now.
Sorry for the long post!
If your users have Adobe Acrobat Reader installed, the following code should do the trick. I recommend Acrobat Reader 11 or lower version. With Acrobat DC the window from Acrobat does not close anymore, that is a "design" decision by Adobe.
Dim pr As New Process()
pr.StartInfo.FileName = "c:\myfile.pdf"
pr.StartInfo.UseShellExecute = True
pr.StartInfo.Verb = "print"
pr.Start()
pr.WaitForExit()
I work with DWG and PDF files for 20+ years. If you are interested in a solution that fully automates all steps you explained in your "wishlist" software and does a ton more feel free to contact me at pdfmagick at gmail dot com
Another way to print PDF files with the Print Drivers dialog is as follows:
Dim starter As ProcessStartInfo
starter = New ProcessStartInfo(<pathToAdobeAcrobatExecutable>, String.Format(" /s /N /P ""{0}""", Filename))
Dim Process As New Process()
Process.StartInfo = starter
Process.Start()
Process.WaitForExit()
Process = Nothing
starter = Nothing
More command line switches are explained here
Adobe Reader Command Line Reference
Regards

Navigate multiple browsers in one command Visual Basic

So, if I have for example three webbrowsers and I want them all to navigate to google.com I have to do it like this:
webbrowser1.navigate("google.com")
webbrowser2.navigate("google.com")
webbrowser3.navigate("google.com")
Is it possible do it in one line of code?
A sub routine:
NavigateTo "google.com"
Which calls
sub NavigateTo(url as string)
webbrowser1.navigate(url)
webbrowser2.navigate(url)
webbrowser3.navigate(url)
end sub
You can use an Array along with a for loop as follows.
Dim webbrowser(3) As WebBrowser
For value As Integer = 0 To 3
webbrowser(value) = New WebBrowser
webbrowser(value).Navigate("www.google.com")
Next
Note:- For only 3 instances, this code may not be necessary, but for 30 it might definitely help.

How to get hardware print?

Can someone tell me how to get the hardware print of my computer, on VB 2010 express, or where and how is that stored? Thanks in advance.
The best way to figure out what hardware is attached to your computer is to use WMI to get the information. Microsoft has created a tool that will create C#, VB.Net and VBScript sample code which you can run with the program and see what the values are, you can then add it to your program. This tool is called the WMI Code Creator. I would start out by exploring the Classes starting with Win32_
Now that I know what you are trying to do I can be a little more specific. The WMI NameSpace you are needing is root\CIMV2 the Class is Win32_DiskDrive or Win32_PhysicalMedia and the Property is SerialNumber. I made a small console test app in Vb.net. It will print out the drive serialnumbers on your PC, if you need it in c# I can modify. There are also numerous other SO Questions about the same subject.
Imports System
Imports System.Management
Module Module1
Sub Main()
For Each sn As String In GetDriveSerialNumber()
Console.WriteLine(sn.Trim)
Next
Console.ReadLine()
End Sub
Function GetDriveSerialNumber() As List(Of String)
Dim snList As List(Of String) = New List(Of String)
Try
Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_DiskDrive")
For Each queryObj As ManagementObject In searcher.Get()
snList.Add(queryObj("SerialNumber").ToString())
Next
Catch err As ManagementException
Throw
End Try
Return snList
End Function
End Module