如何将批处理文件添加到visual basic并按下按钮执行? [英] How can I add a batch file to visual basic and execute by pressing a button?

查看:121
本文介绍了如何将批处理文件添加到visual basic并按下按钮执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2013创建了一个visual basic windows窗体应用程序。

我在添加新项目时创建一个文本文件并将其重命名为run.bat。

And还创建了一个按钮。

我的问题是当我点击按钮时如何执行批处理文件?

此代码不起作用:



I created a visual basic windows form application using Visual Studio 2013.
I create a text file in add new item and rename it to run.bat.
And also created a button.
My question is how can i execute the batch file when i click the button?
This code doesn't work:

Process.Start("run.bat")





ScreenShot

推荐答案

这对我来说没问题如果我也正确地在批处理文件上设置文件属性... 复制输出目录需要设置为总是复制



OR



您需要提供批处理文件的路径,例如
This works fine for me IF I also set the File Properties on the batch file correctly ... Copy to Output Directory needs to be set to Copy always

OR

You need to provide a path to the batch file e.g.
Process.Start("c:\temp\run.bat")


不在我的开发机器前面但是我认为你得到的错误是找不到文件。



你已经要求你的程序执行run.bat但是没有告诉它实际存储了run.bat的位置。 br />


试试这个:

Not in front of my dev machine but I think the error you will get is file not found.

You have asked your program to execute run.bat but not told it where run.bat is actually stored.

Try this:
Dim path As String
   path = System.IO.Path.GetDirectoryName( _
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
Process.start(path & "\Run.bat")





您可能不需要\

路径将在exe运行的任何位置设置,因此批处理文件必须位于同一文件夹中。 EG:c:\ Myprog \ Myyexe.exe和c:\ Myprog \ Run.bat



You may not need the \
The path will be set wherever the exe is ran from, so the batch file will have to be in the same folder. EG: c:\Myprog\Myexe.exe and c:\Myprog\Run.bat


希望下面的代码可以帮到你



hope below code will help you

//To create a batch file
System.IO.StreamWriter SW = new System.IO.StreamWriter("FileName.bat");
SW.WriteLine("FileContent");
SW.Flush();
SW.Close();
SW.Dispose();
SW = null;

//To Run the batch file use below command
System.Diagnostics.Process.Start("FileName.bat");


这篇关于如何将批处理文件添加到visual basic并按下按钮执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆