2

i'm trying to execute vbscript from c#, the vbscript runs always succesfully despite of the fact that i don't have permissions to run this script.

the code:

        //nswp is the User's password
        foreach (char c in uspw)
        {
            password.AppendChar(c);
        }
        Process scriptProc = new Process();
        scriptProc.StartInfo.RedirectStandardOutput = true;
        scriptProc.StartInfo.RedirectStandardError = true;
        scriptProc.StartInfo.Domain = "moia.gov.il";
        scriptProc.StartInfo.ErrorDialog = true;
        scriptProc.StartInfo.UserName = Globals.CURRENT_USER.user_name;
        scriptProc.StartInfo.Password = password;
        scriptProc.StartInfo.UseShellExecute = false;
        scriptProc.StartInfo.FileName = @"cscript";
        scriptProc.StartInfo.Arguments = "//B //Nologo " + pathSave;
        scriptProc.Start();
        scriptProc.WaitForExit();
        scriptProc.Close();

When i'm executing this vbscript manually (by double click on the file icon) i get a permissions error, as i should.

How can i catch this error in c# and throw exception respectively.

Dor Cohen
  • 16,769
  • 23
  • 93
  • 161

1 Answers1

0

You don't have a lot of options when using a different process like you do here. This is about it:

    scriptProc.WaitForExit();
    if (scriptProc.ExitCode != 0) throw new Exception("Script failed");

You can get rich error info by executing the script in-process by using the COM scripting host. That could be overkill, hard to tell.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536