none
How to call a .VBS file in a C# application? RRS feed

  • Question

  • I have a windows application in C# which should run the one text.vbs file.  (NET Framework 4 Client Profile)

     (When I run this file manually from any location, it does the job normally).

    content of text.vbs :  

    Hidden = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Hidden"
    Set Sh = WScript.CreateObject("WScript.Shell")
    St = Sh.RegRead(Hidden)
    If St = 2 Then
    Sh.RegWrite Hidden, 1, "REG_DWORD"
    Else
    Sh.RegWrite Hidden, 2, "REG_DWORD"
    End If
    Sh.SendKeys("{F5}")

    I've tried many options, but none work.

    for example:

     private void Button1_Click(object sender, EventArgs e)
            {
                Process Proc = new Process();
                Proc.StartInfo.FileName = @"cscript";
                Proc.StartInfo.Arguments = "//B //Nologo c:\\text.vbs";
                Proc.Start();
                Proc.WaitForExit();
                Proc.Close();
            }

    or

    private void Button1_Click(object sender, EventArgs e)
            {
                RunVBS(@"C:\text.vbs");
            }
            static void RunVBS(string vbsFilepath)
            {
                var proc = System.Diagnostics.Process.Start(vbsFilepath);
                proc.WaitForExit();
            }

    is it possible to call a .vbs file using c#.net?

    Can anyone please help me on this ?

    Thanks in Advance.


    • Edited by ZoAndrea Monday, February 3, 2020 4:43 PM
    Monday, February 3, 2020 4:40 PM

Answers

  • Use SHGetSetSettings instead (P/Invoke)

    (flag SSF_SHOWALLOBJECTS)

    (no reset/refresh needed)

    • Edited by Castorix31 Monday, February 3, 2020 7:35 PM
    • Marked as answer by ZoAndrea Monday, February 3, 2020 9:51 PM
    Monday, February 3, 2020 6:53 PM
  • To refresh existing Windows Explorer windows, use IShellWindows to enumerate shell windows, check the IWebBrowser2.FullName property to skip IE windows and then call IWebBrowser2::Refresh. 


    Visual C++ MVP

    • Marked as answer by ZoAndrea Monday, February 3, 2020 9:51 PM
    Monday, February 3, 2020 7:21 PM

All replies

  • You can access the registry directly with available .Net Framework API.  See Registry Class
    Monday, February 3, 2020 5:12 PM
  • Thank you RLWA32 but when I try to directly access the registry to do Show hidden files folders and drivers, then the effect is only visible after the restart ... which is not the case when using this VBS file...
    Monday, February 3, 2020 6:20 PM
  • Use SHGetSetSettings instead (P/Invoke)

    (flag SSF_SHOWALLOBJECTS)

    (no reset/refresh needed)

    • Edited by Castorix31 Monday, February 3, 2020 7:35 PM
    • Marked as answer by ZoAndrea Monday, February 3, 2020 9:51 PM
    Monday, February 3, 2020 6:53 PM
  • To refresh existing Windows Explorer windows, use IShellWindows to enumerate shell windows, check the IWebBrowser2.FullName property to skip IE windows and then call IWebBrowser2::Refresh. 


    Visual C++ MVP

    • Marked as answer by ZoAndrea Monday, February 3, 2020 9:51 PM
    Monday, February 3, 2020 7:21 PM
  • I tried but unfortunately I failed to do it. Thank you.
    Monday, February 3, 2020 9:51 PM