locked
How to write multiple lines of commands in command prompt using c#

    Question

  • Hello everyone,

    i was able to start the cmd.exe using System.Diagnostics.Process. but my concern is how to go to specific folder then copy a text file to another folder.

    here is the step 

    First go in some location and find my file C:\somefolder\mytext.txt

    then move the file to another location D:\anotherfoler\mytext.txt

     

    i want to do this thing using C#

    thanks in advance

    Wednesday, May 18, 2011 7:33 AM

Answers

  • Hi, you have to get both paths, path from where the file is now (from where it will be copied), and the path of the destination (to where will be copied). Then you can use File.Copy method:

        string path = @"c:\temp\MyTest.txt";
        string path2 = path + "temp";
    
        try 
        {
          // Create the file and clean up handles.
          using (FileStream fs = File.Create(path)) {}
    
          // Ensure that the target does not exist.
          File.Delete(path2);
    
          // Copy the file.
          File.Copy(path, path2);
          Console.WriteLine("{0} copied to {1}", path, path2);
    
          // Try to copy the same file again, which should succeed.
          File.Copy(path, path2, true);
          Console.WriteLine("The second Copy operation succeeded, which was expected.");
        } 
    
        catch 
        {
          Console.WriteLine("Double copy is not allowed, which was not expected.");
        }
    
    


    Mitja
    • Marked as answer by Magma09 Wednesday, May 18, 2011 1:46 PM
    Wednesday, May 18, 2011 8:09 AM
  • You can do this in the following manner:

          String SourceFolderName = @"C:\Source Folder";
          String DestinationFolderName = @"C:\Destination Folder";
          String FileName = "test.txt";
          String SourceFileName = SourceFolderName + "\\" + FileName;
          String DestinationFileName = DestinationFolderName + "\\" + FileName;
    
          if (File.Exists(SourceFileName) && Directory.Exists(DestinationFolderName))
            File.Move(SourceFileName, DestinationFileName);
    

    • Marked as answer by Magma09 Wednesday, May 18, 2011 1:46 PM
    Wednesday, May 18, 2011 9:25 AM

All replies

  • Hi, you have to get both paths, path from where the file is now (from where it will be copied), and the path of the destination (to where will be copied). Then you can use File.Copy method:

        string path = @"c:\temp\MyTest.txt";
        string path2 = path + "temp";
    
        try 
        {
          // Create the file and clean up handles.
          using (FileStream fs = File.Create(path)) {}
    
          // Ensure that the target does not exist.
          File.Delete(path2);
    
          // Copy the file.
          File.Copy(path, path2);
          Console.WriteLine("{0} copied to {1}", path, path2);
    
          // Try to copy the same file again, which should succeed.
          File.Copy(path, path2, true);
          Console.WriteLine("The second Copy operation succeeded, which was expected.");
        } 
    
        catch 
        {
          Console.WriteLine("Double copy is not allowed, which was not expected.");
        }
    
    


    Mitja
    • Marked as answer by Magma09 Wednesday, May 18, 2011 1:46 PM
    Wednesday, May 18, 2011 8:09 AM
  • You can do this in the following manner:

          String SourceFolderName = @"C:\Source Folder";
          String DestinationFolderName = @"C:\Destination Folder";
          String FileName = "test.txt";
          String SourceFileName = SourceFolderName + "\\" + FileName;
          String DestinationFileName = DestinationFolderName + "\\" + FileName;
    
          if (File.Exists(SourceFileName) && Directory.Exists(DestinationFolderName))
            File.Move(SourceFileName, DestinationFileName);
    

    • Marked as answer by Magma09 Wednesday, May 18, 2011 1:46 PM
    Wednesday, May 18, 2011 9:25 AM
  • Source or Destination folder could already have a backslash. Use Path.Combine instead of concatenation.

    String SourceFileName = Path.Combine(SourceFolderName, FileName);
    String DestinationFileName = Path.Combine(DestinationFolderName, FileName);
    
    
    
    Thursday, May 19, 2011 3:24 PM