moe
moe

Reputation: 5249

How to run VBS script from C# code

All what I am trying to do is to be able to run VBS script from code behind but I am getting this error: “The system cannot find the file specified”. I know the path name and I only need to execute that .vbs script but it is giving me hard time and I am not able to figure out. Please help. Thanks here is my code

System.Diagnostics.Process.Start(@"cscript  //B //Nologo \\loc1\test\myfolder\test1.vbs");

i have updated the code as shown below but i am getting a security warning asking me if i want to open it. Is there a way to not get those kind of warning and just run script without any warnings? here is the updated code:

  Process proc = null;
        try
        {
            string targetDir = string.Format(@"\\loc1\test\myfolder");//this is where mybatch.bat lies
            proc = new Process();
            proc.StartInfo.WorkingDirectory = targetDir;
            proc.StartInfo.FileName = "test1.vbs";
            proc.StartInfo.Arguments = string.Format("10");//this is argument
            proc.StartInfo.CreateNoWindow = false;
            proc.Start();
            proc.WaitForExit();
        }
        catch (Exception ex)
        {
           // Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
        }

Upvotes: 1

Views: 10127

Answers (2)

Pandian
Pandian

Reputation: 9126

Your code working fine for me, I think the error was in your File Path,

Better Confirm the File Path you given is valid or Not..

You can run that file like below also..

Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"cscript"; 
scriptProc.StartInfo.Arguments ="//B //Nologo \\loc1\test\myfolder\test1.vbs";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
scriptProc.Start();
scriptProc.WaitForExit();
scriptProc.Close();

But check your File Path you given..

Upvotes: 3

kemiller2002
kemiller2002

Reputation: 115420

The parameters have to be included separately. There is an arguments field you use to pass arguments to the process.

You can use this as a guide for executing programs with command line from an application.

Upvotes: 3

Related Questions