HStrix
asked on
C#: how to catch error code
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("NE T", "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
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
---
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.ThreadSta te.Aborted ;
but i can't seem to find something like the exit reason
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.ThreadSta
but i can't seem to find something like the exit reason
ASKER
So, I can forget this?
Is there a way to use lastdllerror ...
Is there a way to use lastdllerror ...
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
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
ASKER
Thanks, but it seems that there is no exit state either...
and the exit code is still 2.
and the exit code is still 2.
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
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
ASKER
Thanks,
my new idea is to read the output of net.exe.
my new idea is to read the output of net.exe.
if it will write to the standard out put you can indeed read it through the MS class
ASKER
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..
Since this is event controlled(?) there are new thoughts required...
I expect a huge effort because I've never done this before..
ASKER
For the time-being I use now the following working code
---
try
{
MS.StartInfo.UseShellExecu te=false;
MS.StartInfo.RedirectStand ardOutput= true;
MS.StartInfo.RedirectStand ardError=t rue;
MS.StartInfo.CreateNoWindo w=true;
MS.StartInfo.FileName = "NET";
MS.StartInfo.Arguments = "USE \\\\" + tmpMyComputer + " /user:" + tmpMyUserID + " " + tmpMyPassword;
MS.StartInfo.WorkingDirect ory=Workin gDirectory ;
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..
---
try
{
MS.StartInfo.UseShellExecu
MS.StartInfo.RedirectStand
MS.StartInfo.RedirectStand
MS.StartInfo.CreateNoWindo
MS.StartInfo.FileName = "NET";
MS.StartInfo.Arguments = "USE \\\\" + tmpMyComputer + " /user:" + tmpMyUserID + " " + tmpMyPassword;
MS.StartInfo.WorkingDirect
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..
ASKER
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..