How to Call a Vbscript File in a C# Application

How to call a VBScript file in a C# application?

The following code will execute a VBScript script with no prompts or errors and no shell logo.

System.Diagnostics.Process.Start(@"cscript //B //Nologo c:\scripts\vbscript.vbs");

A more complex technique would be to use:

Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"cscript";
scriptProc.StartInfo.WorkingDirectory = @"c:\scripts\"; //<---very important
scriptProc.StartInfo.Arguments ="//B //Nologo vbscript.vbs";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //prevent console window from popping up
scriptProc.Start();
scriptProc.WaitForExit(); // <-- Optional if you want program running until your script exit
scriptProc.Close();

Using the StartInfo properties will give you quite granular access to the process settings.

You need to use Windows Script Host if you want windows, etc. to be displayed by the script program. You could also try just executing cscript directly but on some systems it will just launch the editor :)

How can I execute VBScript in C#

finally, we decide to migrate to IronPython instead of VBS, vbs is not supported by microsoft.
so it's time for move on

How to run VBS script from C# code

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

Run VBScript from C# with hidden window and capture output

you need to add 1 more line:

process.StartInfo.CreateNoWindow = true;

C# run vbscript file without extension

When you directly call a .vbs file, the system checks the registry to see what executable should handle the task. Instead of leaving it to the system, invoke the scripting host with the adecuated information

cscript.exe //e:vbscript noExtensionFile

or

wscript.exe //e:vbscript noExtensionFile

The //e: switch allows you to indicate the scripting engine that will be used to process the noExtensionFile file

How can I launch a local VBScript with arguments from a C# console application?


System.Diagnostics.Process.Start(
@"C:\my folder\import.vbs",
String.Format("{0} {1}", agr1, agr2));


Related Topics



Leave a reply



Submit