Greg Owen
Over 27 Years of Programming Experience
Programming Examples, Portfolio, and More
Home

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)
      {
      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);
      }
      }
      
    
#CSharp #VisualStudio
Some words from the sponsors...

Add a C# Dropdown to a Bootstrap Modal Dialog

This example combines C# and Bootstrap to create a bootstrap modal dialog containing an asp dropdown and an asp button to retrieve...

Continue Reading

Use C# to Populate Dropdown Lists With XML

The code below opens an xml file, reads the data from the xml file, dynamically adds a label for the dropdown list, and adds the dropdown list to the asp.net page...

Continue Reading

Add Style Properties to ASP.Net Controls to Improve Appearance

The default formatting for ASP.Net controls is very dated.  The default formatting is basically the same as the formatting in Visual Basic 1.0 ...

Continue Reading

Export To A Text File From A DataGridView Control

Add a button control to Form1.
Add a DataGridView Control to Form1....

Continue Reading