We help IT Professionals succeed at work.

How know if code has access to windows resource and vice versa?

Using .NEt 2.0, VS 2005, Windows Forms, VB.Net

How do you know if your VB.Net code has access to a windows resource (such as a printer from the code below). Lately we have been getting system. accessViolationException errors but they are not consistent. Is there a way in .NET to check ?

Thanks
strDefaultPrinter = prtdoc.PrinterSettings.PrinterName
            
            For Each strPrinter In PrinterSettings.InstalledPrinters
                If strPrinter = strDefaultPrinter Then
                    prtExists = True
                End If
            Next strPrinter

Open in new window

Comment
Watch Question

Commented:
Sanmarie,

I'm not sure why you're getting the error, but I do similar checking in my own code (as to what you have posted).  When you actually print to the printer, the best thing to do is trap it in a Try...Catch loop, and either inform the user, or just try looping through it until the print succeeds.

I don't know anyway of checking for access violations, sorry - not to say that there isn't one, I just don't know of one :)

Jake

Author

Commented:

Thanks Jake.  The funny thing is that this error is sporadic. The users will report the error and when we check with them again, they say that the printing was successful the second time. I will look into looping until the printing is successful though.

Thanks

San

Commented:
San,

I've had similar weird errors myself.  My favorite of late is that none of my Crystal Reports will print to only 1 network printer, while still printing to any OTHER network printer on the network when I have my code in My Documents.  If I move the code out to C:\, it all works fine.

Who knows?? :)

Jake

Author

Commented:

Sooner or later, we'll find out.

Author

Commented:
Haven't found the answer but just want to close the queston

Explore More ContentExplore courses, solutions, and other research materials related to this topic.