Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 (used for Show / Hide hidden files folders ...)

VB
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.
is it possible to call a .vbs file using c#.net?
Can anyone please help me on this ?
Thanks in Advance.


What I have tried:

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();
        }
Posted
Updated 5-Feb-20 0:28am
Comments
Richard Deeming 4-Feb-20 8:12am    
What does "none work" mean? Are you getting an error?

You don't need a VBS file to modify the registry; you can do that directly from your .NET code.

You can also send keys to an application from C#:
SendKeys.Send(String) Method (System.Windows.Forms) | Microsoft Docs[^]

Perhaps the problem is that you're sending the keys to the wrong application?
Member 10410972 4-Feb-20 9:52am    
What does "none work" mean? Are you getting an error?
There is no error, simply nothing happens after the code is executed.And I would like to run a vbs file...

You don't need a VBS file to modify the registry; you can do that directly from your .NET code.
I know it can, but then I need to restart machine so that the effect is visible ...

You can also send keys to an application from C#:
Please could you be more specific, I didn't understand ...
Richard Deeming 4-Feb-20 10:32am    
"There is no error, simply nothing happens after the code is executed."
Then you're almost certainly sending the F5 key to the wrong application.

"I know it can, but then I need to restart machine so that the effect is visible ..."
Changing the registry from C# is no different to changing it from VBS. If it works without a restart when you change it from VBS, then it will work without a restart if you change it from C#.

"Please could you be more specific, I didn't understand ..."
You can replace the Sh.SendKeys call in your script with a call to SendKeys.Send in your C# code.
SendKeys.Send(String) Method (System.Windows.Forms) | Microsoft Docs[^]
Member 10410972 4-Feb-20 14:59pm    
I tried something like this but not good enough. I ask for your help.

private void Button1_Click(object sender, EventArgs e)
{
Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced", "Hidden", 1);
SendKeys.Send("{F5}");
}
Richard Deeming 4-Feb-20 15:14pm    
As I said, you're almost certainly sending the F5 to the wrong window.

As per the documentation: "Sends keystrokes to the active application."

Since you're responding to a button click event in your own application, it's your own application which will receive the input.

After you've clicked the button, try manually clicking on the real target window and pressing F5 to make sure the registry change worked.

Assuming it did, you'll need to find a way to identify and activate the target window through code before calling SendKeys.Send.

1 solution

As discussed in the comments, you don't need a VBS script to do this. Everything you do in your script file can be done in C#, using Registry.GetValue[^], Registry.SetValue[^], and SendKeys.Send[^].

The problem is that SendKeys is sending the input to your application's window, because that's the active application when you click the button. You either need to set the focus to the correct window before calling SendKeys.Send, or minimize your application's window to activate the previous window.
 
Share this answer
 
Comments
Maciej Los 5-Feb-20 6:38am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900