?
Solved

C#: how to catch error code

Posted on 2006-07-03
11
Medium Priority
?
704 Views
Last Modified: 2008-02-26
Hello experts,
in my C# windows application (I'm using Visual Studio .Net 2003)
I need to catch the error code (sample 5 = Read-Only)  of the following statement:
---
System.Diagnostics.Process.Start("NET", "USE \\\\" + tmpMyComputer + " /user:" + tmpMyUserID + " " + tmpMyPassword);
---
This is equivalent to
---
Net Use \\<myComputer> /user:<myuserid> <mypassword>
---

If anyone knows how I can catch the error code created by this statement,
please supply an appropriate code snippet.

   Thank you very much for any help.

   HStrix
0
Comment
Question by:HStrix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 44

Accepted Solution

by:
bruintje earned 600 total points
ID: 17029473
Hi HStrix,
----------

if you use something like
using System.Diagnostics;


try
{
  Process MS = new Process();
  MS = Process.Start("NET", "USE \\\\" + tmpMyComputer + " /user:" + tmpMyUserID + " " + tmpMyPassword);
  //now you can lookup something that will be of use when looking at the autocomplete
  //MS.
  //you can read its error output or the exit state
}
catch (Exception ex)
{
    //do something here
}
finally
{

}

wrote this right in the box here so there could be something missing in syntax

----------
bruintje
share what you know, learn what you don't
0
 

Author Comment

by:HStrix
ID: 17029841
Thank you bruintje,
my code looks now as follows:
---
Process MS = new Process();
int tmpErrIntCode = 0;
string tmpErrCatched = "";
try
{
  //Process MS = new Process();
  MS = Process.Start("NET", "USE \\\\" + tmpMyComputer + " /user:" + tmpMyUserID + " " + tmpMyPassword);
  //now you can lookup something that will be of use when looking at the autocomplete
  //MS.
  //you can read its error output or the exit state
  tmpErrIntCode = MS.ExitCode; // <= result = 2 ; where to find exit state?
}
catch (Exception ex)
{
    //do something here
    tmpErrCatched = ex.ToString();  // is not entered
}
finally
{
   //
}
---
Executing the equivalent Net-Use-command leads to error 1385.
I'ld expect this error..

0
 
LVL 44

Expert Comment

by:bruintje
ID: 17029921
sorry there is no exit state, i've looked at the thread properties a few weeks ago trying to get better grip on process running through process.start but wasn't able to find a good handle on it [using a third party interface with sendkeys]

you can look at the thread like
using System.Threading;

                Thread myThread = MS.Threads(0);
                myThread. --> use intellisense here

you can even check the threadstate but it will not give you a reason for exit or wait

                myThread.ThreadState = System.Threading.ThreadState.Aborted;

but i can't seem to find something like the exit  reason
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:HStrix
ID: 17029981
So, I can forget this?

Is there a way to use lastdllerror ...

0
 
LVL 44

Expert Comment

by:bruintje
ID: 17030073
not sure but looking at the ExitCode

http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.exitcode.aspx

there is something interesting about enable raising events on the process so this way you can wait for the process to exit and check the exitcode, they that in a do loop in the msdn sample it could give you something to have a hold on the process exit state

0
 

Author Comment

by:HStrix
ID: 17030127
Thanks, but it seems that there is no exit state either...
and the exit code is still 2.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 17030168
exit code 0 is most of the time a normal exit do you have no list of exitcodes for net use

btw if i read this correctly there are no errorcodes for net.exe

http://groups.google.com/group/alt.msdos.batch/browse_frm/thread/61a346ad5331c064/f689bc2efcb2d1c6
0
 

Author Comment

by:HStrix
ID: 17030254
Thanks,
my new idea is to read the output of net.exe.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 17030295
if it will write to the standard out put you can indeed read it through the MS class
0
 

Author Comment

by:HStrix
ID: 17030376
If it is used as a console call the output gets displayed in the console window.

Since this is event controlled(?) there are new thoughts required...
I expect a huge effort because I've never done this before..

0
 

Author Comment

by:HStrix
ID: 17030840
For the time-being I use now the following working code
---
try
{
MS.StartInfo.UseShellExecute=false;
MS.StartInfo.RedirectStandardOutput=true;
MS.StartInfo.RedirectStandardError=true;
MS.StartInfo.CreateNoWindow=true;
MS.StartInfo.FileName  = "NET";
MS.StartInfo.Arguments = "USE \\\\" + tmpMyComputer + " /user:" + tmpMyUserID + " " + tmpMyPassword;
MS.StartInfo.WorkingDirectory=WorkingDirectory;
MS.Start();
//
errorNet  = "";
do
  {
    if (!MS.HasExited)
       {
          MS.Refresh();
        }
  }
while (!MS.WaitForExit(1000));
//
errorNet = MS.StandardError.ReadToEnd();
}
catch (Exception ex)
{
  //
}
finally
{
  //
  if (MS != null)
  {
    MS.Close();
  }
  //
  if (errorNet != "")
  {
    // message box
  }
}
---
This code cannot be the final solution,
I think the only way is to create a private class for the net use command.

Your help was welcome and helpful..

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question