OMEGA_ReD

How can i execute a batch file

ive tried it with the shell function: Shell("c:\text.bat")

but i keep getting a file not found error.

is there another way for executing an batch file

thanks




Re: Visual Basic General Execute Batch file from application

ahmedilyas

you should not be using shell but use the .NET Framework classes otherwise you are sticking to old (and some buggy) methods and defeating the purpose of upgrading and using the .NET Framework.

you should be using the Process class in the System.Diagnostics namespace like so:

System.Diagnostics.Process.Start("C:\text.bat")

Hope this helps!






Re: Visual Basic General Execute Batch file from application

OMEGA_ReD

hi,

it works but is it possible to add a window style = hidden propperty

what i want to do with the code is run the batch file with username/pass propperty and hidden.

thanks






Re: Visual Basic General Execute Batch file from application

ahmedilyas

yes it is, just set the WindowStyle Property like so....

Dim psi as new System.Diagnostics.ProcessStartInfo("C:\test.bat")

psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden

System.Diagnostics.Process.Start(psi)

hope this helps ;-)






Re: Visual Basic General Execute Batch file from application

OMEGA_ReD

thanks for your reply

ive come up with the following code:

Dim psi As New System.Diagnostics.ProcessStartInfo("C:\test.bat")

psi.UserName = "test"

psi.Password = New System.Security.SecureString()

For Each c As Char In "test"

psi.Password.AppendChar(c)

Next

psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden

psi.UseShellExecute = False

System.Diagnostics.Process.Start(psi)

it launches the application with the username and password but the window shows up again even with the processwindowstyle.hidden propperty






Re: Visual Basic General Execute Batch file from application

ahmedilyas

try setting the "CreateNoWindow" property to true in the psi object.

psi.CreateNoWindow = true






Re: Visual Basic General Execute Batch file from application

OMEGA_ReD

thanks for your reply

it still doesnt work.

is it possible to hide the application after it starts for exaple hide application with title "CMD C:\test.bat"

etc. etc.






Re: Visual Basic General Execute Batch file from application

ahmedilyas

I don't understand why it doesnt work for you as it works fine for me - even without the CreateNoWindow property being set to true.

What does the batch file actually do

can you post the code you are using in VB.NET






Re: Visual Basic General Execute Batch file from application

OMEGA_ReD

what it does is as following:

net use u: \\share\dir
c:\program files...etc\program.exe
net use u: /delete



the program.exe needs the u: share to operate propperly and it also needs admin rights

when i only start the program.exe with System.Diagnostics with username and pass it will not find the share even if my current user has the share mapped.

if i start the commands seperately with the System.Diagnostics the program will not find the share.

my test code:

Code Snippet

Dim psi As New System.Diagnostics.ProcessStartInfo("C:\test.bat")
psi.UserName = "test"
psi.Password = New System.Security.SecureString()
For Each c As Char In "test!"
psi.Password.AppendChar(c)
Next
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
psi.UseShellExecute = False
psi.CreateNoWindow = True
System.Diagnostics.Process.Start(psi)



thanks!





Re: Visual Basic General Execute Batch file from application

ahmedilyas

I see. It helps when you explain every thing ;-)

The reason is because the batch file is running other exe files which causes it to "pop" up in the same window.

will see what else I can find

If I remember correctly, you could use the ShowWindow and give it the SW_HIDE parameter to it to hide the window when you use the FindWindow method. These methods are part of the Win32 API however I would advise not to use them unless absolutely necessary as these are "old" functions which means going deep down into Windows (in a sense) and invoking them - can cause performance issues sometimes. Better to try and do everything in the .NET Framework as much as possible.

http://www.pinvoke.net/default.aspx/user32/FindWindow.html (first find the window)

http://www.pinvoke.net/default.aspx/user32/ShowWindow.html (then hide it)

http://www.pinvoke.net/default.aspx/Constants/SW.html (give ShowWindow the SW_HIDE constant)






Re: Visual Basic General Execute Batch file from application

OMEGA_ReD

hi

again thanks for your reply.

if i for example run notepad.exe it still showsup.

if i remove the username/password and shelexecute properties the notepad.exe will run but it wil run hidden as i can see in the taskmanager.

thanks!





Re: Visual Basic General Execute Batch file from application

ahmedilyas

try not using UseShellExecute property at all - what happens if you do that (dont use it at all, comment it out)

Use the above docs/links to implement the ShowWindow and FindWindow API's - this should then hide the window






Re: Visual Basic General Execute Batch file from application

OMEGA_ReD

the result of not using UseShellExecute is:
The Process object must have the UseShellExecute property set to false in order to start a process as a user.

Code:
Dim psi As New System.Diagnostics.ProcessStartInfo("C:\test.bat")
psi.UserName = "test"
psi.Password = New System.Security.SecureString()
For Each c As Char In "ifajrb123!"
psi.Password.AppendChar(c)
Next
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
'psi.UseShellExecute = False
psi.CreateNoWindow = True
System.Diagnostics.Process.Start(psi)





Re: Visual Basic General Execute Batch file from application

ahmedilyas

hmm yes of course, forgot about that, as you are providing those credentials.

Can you try and implement the Win32 API's suggested and see if that helps, that should now hide the window if you implemented it correctly :-)






Re: Visual Basic General Execute Batch file from application

OMEGA_ReD

ive tried to implement the api's but im doing something wrong with the dll imports i guess.

is it possible if i send you my test code project

it contains only the search window function.

thanks