1

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());
        }
moe
  • 5,149
  • 38
  • 130
  • 197

2 Answers2

3

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.

Community
  • 1
  • 1
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • kevin i just updated my initial post please see above. i am getting a warnings before i am able to execute the script. thanks – moe Apr 23 '13 at 16:27
  • For kicks, try moving the vbs script to a local drive, and see if that gets rid of it. – kemiller2002 Apr 23 '13 at 17:09
3

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

Pandian
  • 8,848
  • 2
  • 23
  • 33
  • i think this worked fine with no problem but if you could tell me if i want to run vbs script that is located on another server how would i do this? i am assuming i have to provide user name and password but don't name how to modify this code? thanks – moe Apr 23 '13 at 16:38
  • @moe - Newer versions of Windows require you to authorize running script files like this. Update your question to reflect this information. – Security Hound Apr 23 '13 at 17:12