Printer Management

Found this site here on code project

http://www.codeproject.com/KB/printing/prntjobcontrollerusingwmi.aspx

Which shows you how to do the relevant things I am after in C# however would it be possible to get the equivelant functions in vb dot net for 2008 ?

As I would like to have a play around with doing what they have done but in vb dot net.

I have never touched C# - at least not to the extent of WMI or anything like that so would be great if someone can port the code for me.

Thanks!!!
LVL 23
Shane Russell2nd Line Desktop SupportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tbsgadiCommented:
Have a look at WMI connections made easy in VB.NET
http://www.devcity.net/PrintArticle.aspx?ArticleID=144


Gary
0
Corey ScheichDeveloperCommented:
There are also code translaters out there paste the c# code in here and it does a pretty good job of making the vb equivalient.

http://www.carlosag.net/Tools/CodeTranslator/

0
Shane Russell2nd Line Desktop SupportAuthor Commented:
Tried that translator which I could not get to work - but found another one which gave me the following code - which is not working.

The data I am passing to the function is the printer name which I am getting from the printers form like so

Jobs.strName = lstPrinters.SelectedItems(0).SubItems(0).Text

The print job ID I am assuming is the Integer Index Value of the Item in the listview I want to cancel and I am getting that value like the 2nd chunk of code below which I have made a public variable so I can call as per the 3rd chunk of code and I put a label on the Jobs form and assigned the lblIndex.Text the same information just to make sure that the information is being passed correctly.

Is the code wrong or am I passing the wrong info into the function or what exactly ?

The cancell all jobs works fine but for some reason I cant seem to cancel individual jobs


Public Function CancelPrintJob(ByVal printerName As String, ByVal printJobID As Integer) As Boolean
        Dim isActionPerformed As Boolean = False
        Dim searchQuery As String = "SELECT * FROM Win32_PrintJob"
        Dim searchPrintJobs As New ManagementObjectSearcher(searchQuery)
        Dim prntJobCollection As ManagementObjectCollection = searchPrintJobs.[Get]()
        For Each prntJob As ManagementObject In prntJobCollection
            Dim jobName As System.String = prntJob.Properties("Name").Value.ToString()
            'Job name would be of the format [Printer name], [Job ID]
 
            Dim splitArr As Char() = New Char(0) {}
            splitArr(0) = Convert.ToChar(",")
            Dim prnterName As String = jobName.Split(splitArr)(0)
            Dim prntJobID As Integer = Convert.ToInt32(jobName.Split(splitArr)(1))
            Dim documentName As String = prntJob.Properties("Document").Value.ToString()
            If [String].Compare(prnterName, printerName, True) = 0 Then
                If prntJobID = printJobID Then
                    'performs a action similar to the cancel
 
                    'operation of windows print console
 
                    prntJob.Delete()
                    isActionPerformed = True
                    Exit For
                End If
            End If
        Next
        Return (isActionPerformed)
    End Function
 
 
' Code to 
Public intIndex As Integer
 
Private Sub lstJobs_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstJobs.MouseClick
        Dim hti As ListViewHitTestInfo = lstJobs.HitTest(e.Location)
        intIndex = hti.Item.Index
 
End Sub
 
'Called like so
 
 Private Sub btnCancelS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelS.Click
 
 
        CancelPrintJob(strName, intIndex)
 
        lblIndex.Text = strName & " " & intIndex
 
    End Sub

Open in new window

0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Shane Russell2nd Line Desktop SupportAuthor Commented:
No I am trying to cancel individual print jobs that has nothing to do with that - thanks for the links though
0
CodeCruiserCommented:
If you read the second link with a bit more attention, it has the same loop as your code above

Dim mo As ManagementObject
For Each mo In queryCollection1
Console.WriteLine(("Printer Driver : " + mo("DriverName").ToString()))
Console.WriteLine(("Document Name : " + mo("Document").ToString()))Console.WriteLine(("Document Owner : " + mo("Owner").ToString()))Console.WriteLine("===========================================")
Next mo

And following code could be rewritten
Dim splitArr As Char() = New Char(0) {}
            splitArr(0) = Convert.ToChar(",")
            Dim prnterName As String = jobName.Split(splitArr)(0)
            Dim prntJobID As Integer = Convert.ToInt32(jobName.Split(splitArr)(1))
            Dim documentName As String = prntJob.Properties("Document").Value.ToString()
            If [String].Compare(prnterName, printerName, True) = 0

as
            Dim prnterName As String = jobName.Split(",")(0)
            Dim prntJobID As Integer = Convert.ToInt32(jobName.Split(",")(1))
            Dim documentName As String = prntJob.Properties("Document").Value.ToString()
            If prnterName.tolower = printerName.tolower then
0
Shane Russell2nd Line Desktop SupportAuthor Commented:
I must be missing the point but that is writing to the console I dont see any code in there that cancels the print jobt that is selected from a listview ?
0
CodeCruiserCommented:
That is the code written in VB.NET which takes you to the edge of the solution. You have to add your own bits to that code like the one i posted above to get it do what you want.
0
Shane Russell2nd Line Desktop SupportAuthor Commented:
I dont know what those bits of code are hence the question as I tried converting the code and that did not work -

Any suggestions on the code ?
0
CodeCruiserCommented:
Did you try the modified code i have posted?

            Dim prnterName As String = jobName.Split(",")(0)
            Dim prntJobID As Integer = Convert.ToInt32(jobName.Split(",")(1))
            Dim documentName As String = prntJob.Properties("Document").Value.ToString()
            If prnterName.tolower = printerName.tolower then
                If prntJobID = printJobID Then
                    prntJob.Delete()
                    isActionPerformed = True
                    Exit For
                End If
            End If
0
Shane Russell2nd Line Desktop SupportAuthor Commented:
Not yet - will try the snippet of code you posted soon.

Thanks for all the help and fast replies.
0
Shane Russell2nd Line Desktop SupportAuthor Commented:
Tried that and it did not work - Please find all the source code etc attached in the zip file.

Most likely very messy and there are some chunks that are most likely not needed - is in visual basic 2008 express.

Any tidying of code or anything you can do to get it working would be great.

Thanks!!!

0
Shane Russell2nd Line Desktop SupportAuthor Commented:
the EE file attachment would not allow me to attach it because of the SLN file extension so please find the zip download here

http://www.2shared.com/file/5965705/9364a6d0/Printers.html

You will have to scroll to the bottom of that page and there should be a link to save the file to your pc.
0
Shane Russell2nd Line Desktop SupportAuthor Commented:
any updates??
0
CodeCruiserCommented:
Could you not test the code yourself and see what happens? Post any errors here.
0
Shane Russell2nd Line Desktop SupportAuthor Commented:
I am getting a lot of code suggestions and I decided to post what I have currently ( from a week ago ) and I have tried all the code suggestions - I think I wanted the delete button ( as in select one print job ) and it cancel only the selected print job - not all of them.

I could not get this to work hence the question and hence posting the attached code.

I tried what was suggested so far but still no luck.

If someone could just get the delete selected job working I would be happy with that.

Thanks
0
CodeCruiserCommented:
In my code above, you see following block

If prntJobID = printJobID Then
                    prntJob.Delete()
                    isActionPerformed = True
                    Exit For
End If

All you need to do is populate the printJobID variable with the selected job ID and it should do the trick.
0
Shane Russell2nd Line Desktop SupportAuthor Commented:
so instead of having


If prntJobID = printJobID Then

I would have


If prntJobID =Selected_ID_From_List_View Then

code here to delete job ?
0
CodeCruiserCommented:
CORRECT
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shane Russell2nd Line Desktop SupportAuthor Commented:
Thanks for the help although it took a long time to get there ( most likely my fault ) as not brilliant with coding.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.