VBScript in VB.NET Windows Form

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 19073 Views - Last Post: 24 February 2011 - 05:45 PM Rate Topic: -----

#1 bcetin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-June 10

VBScript in VB.NET Windows Form

Posted 15 June 2010 - 11:30 PM

Hi everyone,

As a system engineer, I very well know how to do various tasks with VBScript. I have many scripts which makes my life easier through the day. I want to visualize these scripts in Windows Forms created for different situations. I just installed Visual Studio 2008 and created some Windows Forms from VB.NET templates. So, if you say this can be done better with C# forms or another language, I can go that way. I chose VB.NET because I know it a bit so I don't have to learn another language from scratch.

So, my question is how can I execute these VBScript codes inside the Windows Form (of course in a button click event). I don't want to execute them by pointing to the VBS file, I want them to be in the EXE file when I compile the form so if I hand out the form to someone else, they cannot modify the scripts.

Thanks,
Batuhan

This post has been edited by bcetin: 15 June 2010 - 11:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: VBScript in VB.NET Windows Form

#2 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: VBScript in VB.NET Windows Form

Posted 15 June 2010 - 11:50 PM

Well you could do it this:

Private Sub Button1_click
Process.Start("<The path of your .vbs file here>")
End Sub


Was This Post Helpful? 0
  • +
  • -

#3 Goethals  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 145
  • Joined: 14-April 09

Re: VBScript in VB.NET Windows Form

Posted 16 June 2010 - 05:42 AM

You can store your vb-file withing your assembly using embedded resources.

To my knowledge, you'll have extract them to your hard drive anyway
if you want to run them. Nothing stops you from deleting that file again
after your done though.

Is it a big problem when your users will be able to READ the file,
with much effort, or are you only trying to stop them from modifying
the file?

If so, is there no alternative to write VB code for the tasks your VB files
are executing? (Generally VB is much more performant and controllable then
VBScript.)

If not, i suggest you google the web for:

- Process.Start
- Embedded Resources

And a play a bit with the concepts.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#4 bcetin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-June 10

Re: VBScript in VB.NET Windows Form

Posted 16 June 2010 - 08:01 AM

View PostJack Eagles1, on 15 June 2010 - 10:50 PM, said:

Well you could do it this:

Private Sub Button1_click
Process.Start("<The path of your .vbs file here>")
End Sub



Thanks but as I stated in my post, I don't want to do this by pointing the vbs files. I don't want the VBS files to be modified by the people who will use the forms.


View PostGoethals, on 16 June 2010 - 04:42 AM, said:

You can store your vb-file withing your assembly using embedded resources.

To my knowledge, you'll have extract them to your hard drive anyway
if you want to run them. Nothing stops you from deleting that file again
after your done though.

Is it a big problem when your users will be able to READ the file,
with much effort, or are you only trying to stop them from modifying
the file?

If so, is there no alternative to write VB code for the tasks your VB files
are executing? (Generally VB is much more performant and controllable then
VBScript.)

If not, i suggest you google the web for:

- Process.Start
- Embedded Resources

And a play a bit with the concepts.

Hope this helps.


As a last resort, I will port my VBS codes to VB.NET. I did not start doing that because I don't trust my knowledge of VB.NET. Some scripts do complicated tasks using WMI or other controls. I will google for the concepts you stated but if what you say is true, I will start rewriting the codes in VB.NET. You will be seeing me much more in these forums then :)
Was This Post Helpful? 0
  • +
  • -

#5 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: VBScript in VB.NET Windows Form

Posted 16 June 2010 - 09:18 AM

Embed your .vbs file as a resource
WriteAllBytes to a temp file with a random name
When finished with it, Kill WSCript.exe and delete the file.
Was This Post Helpful? 1
  • +
  • -

#6 bcetin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-June 10

Re: VBScript in VB.NET Windows Form

Posted 17 June 2010 - 12:22 AM

View PostJack Eagles1, on 16 June 2010 - 08:18 AM, said:

Embed your .vbs file as a resource
WriteAllBytes to a temp file with a random name
When finished with it, Kill WSCript.exe and delete the file.


I'm trying to do this but I have a problem. I have embedded my VBS file, written it to a temp file in the form of GUID.vbs, executed and deleted the file. The problem is the vbs file is deleted before it's finished and I'm getting an error saying "The file does not exists". When I debug, it works perfectly. It just doesn't wait for the script to finish. The code I'm using is:

Dim sr As New System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("SystemConfig.script1.vbs"))
Dim vbScript1Code As String = sr.ReadToEnd
sr.Close()
Dim Script1GUID As Guid = Guid.NewGuid()
Dim Script1FileName As String = Script1GUID.ToString() & ".vbs"
Script1FileName = Application.StartupPath & "\" & Script1FileName 
Dim sw As New System.IO.StreamWriter(Script1FileName)
sw.Write(vbScript1Code)
sw.Close()
Process.Start(Script1FileName)
Dim Script1FileInfo As New System.IO.FileInfo(Script1FileName)
Script1FileInfo.Delete()


This post has been edited by bcetin: 17 June 2010 - 12:23 AM

Was This Post Helpful? 0
  • +
  • -

#7 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: VBScript in VB.NET Windows Form

Posted 17 June 2010 - 03:05 AM

Well... You are probably getting the error because the file is still in use by WSript.exe.

Try this code:

Dim sr As New System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("SystemConfig.script1.vbs"))
Dim vbScript1Code As String = sr.ReadToEnd
sr.Close()
Dim Script1GUID As Guid = Guid.NewGuid()
Dim Script1FileName As String = Script1GUID.ToString() & ".vbs"
Script1FileName = Application.StartupPath & "\" & Script1FileName 
Dim sw As New System.IO.StreamWriter(Script1FileName)
sw.Write(vbScript1Code)
sw.Close()
Process.Start(Script1FileName)
'Wait for one second for the script to run. Change 1000 (1 second) to whatever you need for it to run
System.Threading.Thread.Sleep(1000)

'Incase WScript is still accessing the file, search for WScript process and kill it if it exists:
For Each P as Process in Process.GetProcesses
  If P.ProcessName.Tolower = "WScript.exe".Tolower Then
     P.Kill
  End If
Next

'Now we can delete the file, as it will no longer be in use.
Dim Script1FileInfo As New System.IO.FileInfo(Script1FileName)
Try
    Script1FileInfo.Delete()
Catch Ex as Exception
    MessageBox.Show(ex.Tostring)
End Try


This post has been edited by Jack Eagles1: 17 June 2010 - 03:07 AM

Was This Post Helpful? 0
  • +
  • -

#8 Goethals  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 145
  • Joined: 14-April 09

Re: VBScript in VB.NET Windows Form

Posted 17 June 2010 - 04:31 AM

There must be a way you can track the script if its done yet or not ...
Guessing the script will only take a second seems so ... unreliable ...
Doesn't the process you run your script on exits automaticly when the
script stops running?

So you could monitor your process till it has exited, before proceeding
to delete the file.

'' // Create the file at TempPath
Dim Process As Process = Process.Start(TempPath)
While Not Process.HasExited
    Threading.Thread.Sleep(1000)
End While
System.IO.File.Delete(TempPath)



(You gotta test this out though.)
Was This Post Helpful? 0
  • +
  • -

#9 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: VBScript in VB.NET Windows Form

Posted 17 June 2010 - 04:48 AM

Well you could add a timer which ticks every few seconds, to check if WScript is still running. Then if it's not running, delete the file.
Was This Post Helpful? 0
  • +
  • -

#10 bcetin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-June 10

Re: VBScript in VB.NET Windows Form

Posted 18 June 2010 - 04:33 AM

Thanks for the answers.

View PostGoethals, on 17 June 2010 - 03:31 AM, said:

There must be a way you can track the script if its done yet or not ...
Guessing the script will only take a second seems so ... unreliable ...
Doesn't the process you run your script on exits automaticly when the
script stops running?

So you could monitor your process till it has exited, before proceeding
to delete the file.

'' // Create the file at TempPath
Dim Process As Process = Process.Start(TempPath)
While Not Process.HasExited
    Threading.Thread.Sleep(1000)
End While
System.IO.File.Delete(TempPath)



(You gotta test this out though.)


I'm trying this one but "HasExited" does not come up in the choices after I write "While Not Process."

JackEagles1, I also tried tracking the "wscript" process which runs the script, checked if it is running and delete the file when it stops. That worked perfectly but potentially dangerous because there may be another automated VBscript running in background (maybe a backup script that will run for an hour) and my program will be waiting for that script to finish. So I'm stuck again. Any ideas appreciated.
Was This Post Helpful? 0
  • +
  • -

#11 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: VBScript in VB.NET Windows Form

Posted 18 June 2010 - 06:05 AM

How about checking if the process based upon how long it has been running, so if it has just started running, and it is wscript.exe, we can assume that it's the one which your program has started. However, if it has been running for an hour or something, then we can assume that it's your backup process. Then we can check if the one which has just started has exited or not.. and then repeat this untill it has exited.

I'm currently at school so no VB, but when I get home I'll write some code for you.

This post has been edited by Jack Eagles1: 18 June 2010 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#12 bcetin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-June 10

Re: VBScript in VB.NET Windows Form

Posted 18 June 2010 - 07:27 AM

Oh, I don't know what was wrong last time but I got this working.

Dim myProcess As Process = Process.Start(Script1FileName)
While Not myProcess.HasExited
    Threading.Thread.Sleep(1000)
End While
Dim Script1FileInfo As New System.IO.FileInfo(Script1FileName)
Script1FileInfo.Delete()



But the method that Jack tells about is also interesting:

View PostJack Eagles1, on 18 June 2010 - 05:05 AM, said:

How about checking if the process based upon how long it has been running, so if it has just started running, and it is wscript.exe, we can assume that it's the one which your program has started. However, if it has been running for an hour or something, then we can assume that it's your backup process. Then we can check if the one which has just started has exited or not.. and then repeat this untill it has exited.

I'm currently at school so no VB, but when I get home I'll write some code for you.


I will myself try something to do this but if you can provide some code for guidance when you get home, I'll be more than happy.

This post has been edited by bcetin: 18 June 2010 - 08:04 AM

Was This Post Helpful? 0
  • +
  • -

#13 Goethals  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 145
  • Joined: 14-April 09

Re: VBScript in VB.NET Windows Form

Posted 18 June 2010 - 12:59 PM

Is it possible the script that failed required user input?
(Be it only the request of a keystroke?)
If you run them manually, do they close automaticly?

You could also work with a timeout, to check if they gang.
After a specified time kill it if its finished or not.

The process node should always refer to the one you started btw.
Assuming a process is yours because its named the same and
is active simultanuous is dangerous.

Dim myProcess As Process = Process.Start(Script1FileName)
Dim LimitDate AS DateTime = DateTime.Now.AddMinutes(5)
While Not myProcess.HasExited
    If DateTime.Now > LimitDate Then
        myProcess.Kill()
        Exit While
    Else
        Threading.Thread.Sleep(1000)
    End If
End While
Dim Script1FileInfo As New System.IO.FileInfo(Script1FileName)
Script1FileInfo.Delete()


Was This Post Helpful? 1
  • +
  • -

#14 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: VBScript in VB.NET Windows Form

Posted 18 June 2010 - 02:45 PM

Nice one Goethals, you got there before me!
Was This Post Helpful? 0
  • +
  • -

#15 Guest_Sandeep Verma*


Reputation:

Re: VBScript in VB.NET Windows Form

Posted 24 February 2011 - 05:41 PM

View PostJack Eagles1, on 17 June 2010 - 03:05 AM, said:

Well... You are probably getting the error because the file is still in use by WSript.exe.

Try this code:

Dim sr As New System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("SystemConfig.script1.vbs"))
Dim vbScript1Code As String = sr.ReadToEnd
sr.Close()
Dim Script1GUID As Guid = Guid.NewGuid()
Dim Script1FileName As String = Script1GUID.ToString() & ".vbs"
Script1FileName = Application.StartupPath & "\" & Script1FileName 
Dim sw As New System.IO.StreamWriter(Script1FileName)
sw.Write(vbScript1Code)
sw.Close()
Process.Start(Script1FileName)
'Wait for one second for the script to run. Change 1000 (1 second) to whatever you need for it to run
System.Threading.Thread.Sleep(1000)

'Incase WScript is still accessing the file, search for WScript process and kill it if it exists:
For Each P as Process in Process.GetProcesses
  If P.ProcessName.Tolower = "WScript.exe".Tolower Then
     P.Kill
  End If
Next

'Now we can delete the file, as it will no longer be in use.
Dim Script1FileInfo As New System.IO.FileInfo(Script1FileName)
Try
    Script1FileInfo.Delete()
Catch Ex as Exception
    MessageBox.Show(ex.Tostring)
End Try



Although this thread it too old but i think someone else would find it helpful.
We can always use 'Using' if we want to close a procedure. Its always good to use it specially when we are opening any connection(Ex. FTP) or we are trying to read/write a file. Its auto flush.
here we can always use this:
From the point where Jack had started Process.Start()
           Using scriptProcess As New Process()
                scriptProcess.StartInfo.FileName = "CScript.exe"
                scriptProcess.StartInfo.Arguments = Script1FileName
                scriptProcess.StartInfo.UseShellExecute = True
                Try
                    scriptProcess.Start()
                    scriptProcess.WaitForExit()
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            End Using


I am more comfortable in using CScript.exe than WScript.exe If you dont want this you can leave the default file name. I am also using ScriptProcess.WaitForExit() to wait till the process gets completed. And by doing this I make sure that i perform the next action only after it gets completed.

Soon after this we can delete the file that we just created and BINGO!!!

By the way... Thanx a lot JACK and Goethals.. Your discussion helped me a lot. People like you make the world easier.. Thanx a ton...
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2