Dynamically Create and Execute a Batch File Using Visual Studio
Posted on February 23, 2011 by Greg Owen — Leave a comment
Create a directory called “c:\bat”.
Create a directory called “c:\zip”.
Copy some .zip files to “c:\zip”
Create a new Visual Studio C# application.
Add a button to Form1.
Double click on button1 and copy and paste the code below into the Click event.
private void button1_Click(object sender, EventArgs e)
{
try
{
//This will create a new .bat file in the bat directory.
System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\bat\dirlist.bat");
//The code below will write lines to the .bat file.
//The dir command is used in a Command to list files in the specified directory.
//The > command in this case sends the list to a text file.
file.WriteLine(@"dir C:\Zip\*.zip > C:\bat\dir.txt");
file.WriteLine("exit");
file.Close();
//The System.Dignostics.Process Process Class allows a Visual Studio Program
//to execute another application
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
//This line executes the .bat file created above.
proc.StartInfo.FileName = @"C:\bat\dirlist.bat";
proc.Start();
}
catch (System.Exception err)
{
System.Windows.Forms.MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
'개발 > C#' 카테고리의 다른 글
C# <이벤트> 통신(Uart,CAN 등) 에서 실시간 값전달 (0) | 2019.09.30 |
---|---|
C# Dictionary sort by value (딕셔너리 정렬, 값으로) (0) | 2019.07.16 |
C# contextmenustrip Control 찾기 (0) | 2019.07.12 |
C# 패널 스크롤바 키보드로 움직이기 (0) | 2019.07.09 |
C# 압축파일에서 압축풀지 않고 이미지 가져오기 (0) | 2019.04.29 |
C# [미디어] 코덱 (0) | 2019.04.23 |
C# 대용량 파일에서 마지막 라인 읽기 (0) | 2019.04.10 |
C# [시스템] delay 함수 (0) | 2019.03.23 |