0

I have been looking all over but cannot find a definitive answer/solution, or any solution I try fails. I am making a WinFormApp that calls an embedded .vbs file to script a program I use at work. This is what my Project Solution looks currently: Project Solution

The test.vbs file is set as an embedded resource in its file properties. Here is the different code I have tried to use to run the script from the form:

private void button1_Click(object sender, EventArgs e)
    {
        string path = Path.Combine(Path.GetTempPath(), "test.vbs");
        File.WriteAllBytes(path, Properties.Resources.Test);
        Process.Start(path);

        string path2 = Path.Combine(Path.GetTempPath(), "test.vbs");
        System.Diagnostics.Process.Start(@"cscript //B //Nologo " + path2 + "");
    }

Here is what my test.vbs file is:

dim thing
thing = "It did something!"

Wscript.Echo thing

The test.vbs file is primarily meant as a proof of concept to make sure I can at least run a .vbs file. The test.vbs file compiles fine outside of the WinForm.

Most of the time I receive 'The system cannot find the file specified' as the error message. I have read that it may be easier to just convert my .vbs file to C# but they are GUI scripting for SAP and all of SAP's libraries seem primarily set us for .vbs files.

I am still relatively new to C# so I may be way off with this so please tell me if I am. If there is another question that fixes my issue please link it.

Thank you for your time!

EDIT #1 Code compiles and seems to run.

string path2 = Path.Combine(Path.GetTempPath(), "test.vbs");
        var startInfo = new ProcessStartInfo
        {
            WorkingDirectory = "" + path2 + "",
            FileName = @"cscript"
        };

However the script does not seem to be outputting to the cmd...

  • Try with set WorkingDirectory and call your process and check if it's working https://stackoverflow.com/a/114937/713789 – Anirudha Gupta Jan 10 '18 at 04:45
  • See https://social.msdn.microsoft.com/Forums/en-US/adcae113-4758-481a-a367-60d5d14d97d6/this-is-how-to-turn-vbs-and-js-files-into-exe-files-from-the-command-line-without-third-party-tools?forum=scripting on how to run VBScript within your process. It will only work in 32bit executables. – ACatInLove Jan 10 '18 at 04:47
  • @Adrian Do you mean something like this? `string path2 = Path.Combine(Path.GetTempPath(), "test.vbs"); var startInfo = new ProcessStartInfo(); startInfo.WorkingDirectory = "" + path2 + ""; startInfo.FileName = "test.vbs"; Process proc = Process.Start(startInfo);` sorry code in comments does not look good..... – Jackson Geiger Jan 10 '18 at 04:53
  • @JacksonGeiger your code look fine, make sure you are calling "cscript" in your code. – Anirudha Gupta Jan 10 '18 at 05:09
  • @Adrian Like this? `StartInfo.FileName = @"cscript";` – Jackson Geiger Jan 10 '18 at 05:26
  • @Adrian that seemed to have worked I am no longer getting any run time errors! However is does no seem to be running the file anymore. The command prompt opens and closes immediately with no text output.... so not sure if fully working... any ideas? – Jackson Geiger Jan 10 '18 at 05:47
  • You can plug https://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited(v=vs.110).aspx event – Anirudha Gupta Jan 10 '18 at 06:04

1 Answers1

0

Your process start command is incorrect. Here is the correct version

        System.Diagnostics.Process.Start("cscript", @"//B //Nologo " + path2 + "");
Cinchoo
  • 6,088
  • 2
  • 19
  • 34