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

SanmarieAsked:
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.

jake072Commented:
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
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
SanmarieAuthor 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
0
jake072Commented:
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
0
SanmarieAuthor Commented:

Sooner or later, we'll find out.
0
SanmarieAuthor Commented:
Haven't found the answer but just want to close the queston
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
.NET Programming

From novice to tech pro — start learning today.