Dynamically Create and Execute a Batch File Using Visual Studio

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)
        //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");

        //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";
    catch (System.Exception err)
        System.Windows.Forms.MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

Over 20 years programming experience 5 years Oracle SQL/Stored Procedure programming Over 10 years SQL 2000/2005/2008 Server SQL/Stored Procedure programming Over 18 years Visual Basic 3.0/4.0/5.0/6.0/.Net/2003/2005/2008/2010/2015 Over 10 years ASP.Net Over 7 years Visual C# Over 20 years Microsoft Office and VBA HTML JAVA COBOL EASYTRIEVE FORTRAN

Tagged with: , , , , ,
Posted in Visual Studio Code Examples

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: