none
Why can`t i chane the directory while using windows console via c# programm RRS feed

  • Question

  • using System;
    using System.Text;
    using System.Diagnostics;
    using System.IO;

    public class MyClass
    {
     static void Main(string[] args){
      ProcessStartInfo psi = new ProcessStartInfo();
      Process process = new Process();              
          process.StartInfo.FileName = "cmd.exe";     

     while(true){
     
     string command= Console.ReadLine();
          
          
          process.StartInfo.Arguments = "/c " + command;
          
          process.StartInfo.UseShellExecute = false;
          
          process.StartInfo.RedirectStandardError = true;
          process.StartInfo.RedirectStandardOutput = true;   
          
          process.StartInfo.CreateNoWindow = true;    
          process.EnableRaisingEvents = true;               
      
      process.Start();

      Console.WriteLine(process.StandardOutput.ReadToEnd());
      
      process.WaitForExit();


    if(command.Equals("quit")){break;}

    }
     
     }}



    Saturday, January 6, 2018 8:05 AM

All replies

  • If you want to change the current directory, use Directory.SetCurrentDirectory.

    Change it back or simply get the current directory, Directory.GetCurrentDirectory.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, January 6, 2018 10:17 AM
    Moderator
  • can i do like this

    	  Directory.SetCurrentDirectory(Directory.GetCurrentDirectory());
    

    Directory.SetCurrentDirectory(Directory.GetCurrentDirectory());

     
    Saturday, January 6, 2018 11:03 AM
  • Note sure why you would do that, what would be more likely is where C:\Data can be a variable with a folder name for instance or taken from a command line arg in a console app.

    var currentDirectory = Directory.GetCurrentDirectory();
    Directory.SetCurrentDirectory(@"C:\Data");
    // do something
    Directory.SetCurrentDirectory(currentDirectory);


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, January 6, 2018 11:59 AM
    Moderator
  • mmmm thnx for helping  newbie me/ i just wanna stay on a last changed directory where i moved/ but when i set current directory to "D:/" for example, aand i type "cd C:/" than "cd" to check where iam  it shows "D:/". but i wanna go  "C:/" 
    Saturday, January 6, 2018 1:09 PM
  • Why are you using "cd", the methods I provided do this.

    Use SetCurrentDirectory(@"C:\");

    To change directory

    Example

    static void Main(string[] args)
    {
        if (args != null)
        {
            Console.WriteLine(Directory.GetCurrentDirectory());
            Directory.SetCurrentDirectory(args[0]);
            Console.WriteLine(Directory.GetCurrentDirectory());
            Console.ReadLine();
        }    
    }
    

    For this example I passed the command line parameter as followed from project properties.

    Results


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, January 6, 2018 2:22 PM
    Moderator
  • //Could you plzzzz compile the below code and run it. then change directory without changing the code??? thnx )))

    using System;
    using System.Text;
    using System.Diagnostics;
    using System.IO;

    public class MyClass
    {
     static void Main(string[] args){
      ProcessStartInfo psi = new ProcessStartInfo();
      Process process = new Process();              
          process.StartInfo.FileName = "cmd.exe";     

     while(true){
     
     string command= Console.ReadLine();
          
          
          process.StartInfo.Arguments = "/c " + command;
          
          process.StartInfo.UseShellExecute = false;
          
          process.StartInfo.RedirectStandardError = true;
          process.StartInfo.RedirectStandardOutput = true;   
          
          process.StartInfo.CreateNoWindow = true;    
          process.EnableRaisingEvents = true;               
      
      process.Start();

      Console.WriteLine(process.StandardOutput.ReadToEnd());
      
      process.WaitForExit();


    if(command.Equals("quit")){break;}

    }
     
     }}



    Saturday, January 6, 2018 4:46 PM
  • i wana my programm to behave like console))) but it crashes when i change directory like "cd .." or "cd C:/Users" or "cd someFolder" BUT it jumps to the directory which was set BY METHOD. i wana change directory after compilation. to be able to change dir as much as i want while running my programm.exe.    
    Saturday, January 6, 2018 5:25 PM
  • https://stackoverflow.com/questions/47247192/change-the-working-directory-of-a-running-process-with-c-sharp

    seems immposible or what???

    Saturday, January 6, 2018 5:53 PM
  • Probably a command like “D:” has effect inside the started process, but not for your program. Maybe try such approach:

    while( true )
    {
       string command = Console.ReadLine();
    
       if( Regex.IsMatch( command, "[a-z]:", RegexOptions.IgnoreCase ) )
       {
          Environment.CurrentDirectory = command;
          continue;
       }
    
       . . .
    }
    

    Saturday, January 6, 2018 7:03 PM
  • Hello David,

    Because you start the cmd.exe each time in your "while" loop statement. When you change the directory and next the "CD" command you enter is a new target of cmd.exe. so the "cd" command result is still the current directory.

    And the StandardOutput.ReadToEnd method must wait the input stream closed or cmd.exe existed, otherwise the method will block the main thread forever. So you couldn't implement the "interact with cmd.exe" feature. As for a workaround you could execute the multi command at once, there command use "&" connect each other. The input should be like this.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 8, 2018 11:22 AM
    Moderator
  • Are you trying to write a program that works like "cd"?  That's just not possible.  The "current directory" is part of the state of the console application itself.  When a new process is created, it gets a copy of that state.  Any changes you make are not reflected back.  The same thing is true of environment variables.  Your process cannot change the parent's environment.

    Note that "cd", "chdir", "pushd" and "popd" are NOT external commands.  You will not find exes for them.  Those are all internal commands, interpreted directly by cmd.exe.  They have to be, in order to change cmd's internal state.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Tuesday, January 9, 2018 7:09 PM
  • Hello David,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and cooperation.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 12:40 PM
    Moderator