VBScript within VB.NET question
I am a noobie at VB.NET, but I am trying to figure something out.
I have a few VBScript files I wrote and I know some VB.NET. I am trying to include these VBScript files within my compiled program and have it display the strings.
Basically, I want the VBScript files to run and then display them on a VB.NET form.
I am using Visual Basic .NET.
Thanks! :)
Re: VBScript within VB.NET question
I'm a little confused by your post.
Are you wanting to just display the vbScript code as text somewhere in your program? Or are you wanting the vbScript to execute while on your form??
As far as I know, vbScript only runs through a web browser. You could use a webBrowser control on your form and run the vbScript through it but I don't think you can just run vbScript right off a form? ( like response.write("whatever...") wouldn't work on your form.)
Re: VBScript within VB.NET question
VBScript
---
Code:
sProgramImageName1 = "firefox.exe"
Set oShell = CreateObject("WScript.Shell")
sComputer = "." ' use "." for local computer
Set oWmi = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& sComputer & "\root\cimv2")
sWmiQuery = "select Name from Win32_Process where name='" _
& sProgramImageName1 & "'"
Set oQResult = oWmi.ExecQuery(sWmiQuery)
If oQResult.Count = 0 Then
RunningProgram = sProgramImageName1
Else
RunningProgram = "test"
End If
VB.NET
---
Code:
Module Computer
Overloads Sub Main(ByVal args() As String)
Dim txtFile As System.IO.StreamWriter
Dim RunningProgram As String
Try
txtFile = New System.IO.StreamWriter("C:\code\test.txt")
txtFile.WriteLine(RunningProgram)
txtFile.Close()
Catch ex As Exception
Console.WriteLine("An error occured:" & vbCrLf & "Error text: " & ex.Message & vbCrLf & "Stack trace: " & ex.StackTrace & vbCrLf)
End
End Try
End Sub
End Module
When I run it, I see the txt file - but nothing written inside.
I'm guessing I need to use Microsoft.VisualBasic, but just not quite sure how to! Thanks! :)
Re: VBScript within VB.NET question
You have ...
Quote:
Dim RunningProgram As String
Try
txtFile = New System.IO.StreamWriter("C:\code\test.txt")
txtFile.WriteLine(RunningProgram)
Where do you set RunningProgram to anything?
You never set the string to = anything in vb.net.
Also, where do you have the vbScript code in your application? How is it getting into it? At first I thought you were reading it in as a text file. But I can't tell from the code.
Are you trying to use the RunningProgram variable from the vbScript to set the RunningProgram variable from VB module?? Are you passing the vbScript RunningProgram variable to the console application when you launch it?
Are you trying to write the entire vbScript code to the text file or just the RunningProgram variable from the vbScript code? (again. I can't tell)
If you want to use the RunningProgram vbScript variable and set the RunningProgram variable in vb.net to equal it, then I think the way you've coded this, you're creating what amounts to be a local RunningProgram Variable when you dimension it again as a string in .Net. So you can't use the RunningProgram variable from vbScript because RunningProgram is now a vb.net string variable that isn't equal to anything. - which is why nothing is wrote to the file also.
I don't know that I understand exactly what you're trying to do to be of much help. :sick:
Re: VBScript within VB.NET question
Take a look at
http://www.derdy.com/vbnet.jpg
Like I said, I can build it - but I want to be able to use the Variable in the .NET code that I got from the VBScript code.
Is this possible? :afrog:
Re: VBScript within VB.NET question
Okay.
I'm not sure if it is possible. But if it is, you'd have to make RunningProgram a global variable. The way you've coded this, it is a local variable in sub main and you can't use the variable from the vbScript.
A class is global. So, maybe the vbScript is too? Try leaving out the name of your RunningProgram variable in the VB.NET. If the vbScript file is global, the variable would still be available. So no need to DIM RunningProgram again.
See if you get an error stating that RunningProgram is undefined when you use it in your WriteLine statement.
VB Code:
txtFile.WriteLine(RunningProgram)
If you get an error (it will not compile and run in debug) then the .Net isn't seeing the variable.
Start there...
Re: VBScript within VB.NET question
I don't think that's going to work...
You've got me curious. I'm going to play with it some... :)
Re: VBScript within VB.NET question
Quote:
Originally Posted by darth vador
If you get an error (it will not compile and run in debug) then the .Net isn't seeing the variable.
Start there...
.NET is not seeing the variable at all. If you could find a way to do this - I would love you forever!
I know what some of you may be thinking, "oh, just switch to VB.NET." Well, when you have thousands of lines of VBScript - it makes it most difficult. I'm trying to learn! :)
Re: VBScript within VB.NET question
Question. You're using a console app to do this now.
You had to be using an asp page before. correct?
Are you needing to do this with a console or desktop app now? It can't be ASP.NET?
I'm starting to think the only way you may be able to do this with a console app or desktop app with forms, is to create a web service first, using the vbScript? - maybe
Then consume it with a console/desktop app. In which case, you'd sure get an opportunity to learn NET pretty well. ;) You'd write a web service and the service consumer app too. :)
On 2005 Express you can not add a vbScript file to either a .NET or ASP.NET project. I don't know about 2005 studio, I don't have it. I played around with 2003 but couldn't get it to work within a console app.
Re: VBScript within VB.NET question
I created a desktop app using forms. No ASP here - only PHP :)
Currently, I use a timer script..
Code:
If Timer2.Interval = 240000 Then
Dim oShell, a
oShell = CreateObject("Wscript.Shell")
a = "c:\code\test.vbs"
oShell.run(a, 3, True)
End If
but, it would be nice to be able to just compile all the vbs files into the .NET application and call them from there. Also, less of a chance to have people steal my code ;)
Re: VBScript within VB.NET question
Also, I found http://www.adminscripteditor.com/editor/scriptpackager/ which is an awesome program, but it costs $100.
Anybody know of another solution? Thanks!
Re: VBScript within VB.NET question
Re: VBScript within VB.NET question
If I uderstand what you are trying to do correctly I did something similar to get the free disk space on servers.
the method I used was quick, dirty and not good practice (a bit ashamed of it) but it did work.
I used vb to make a text / .wsf file and shelled out to dos to run the script.
when the shell exited I read the output file of the script and pick out what i wanted.