为什么我不能通过c#programm在使用Windows控制台时查看目录 [英] Why can`t i chane the directory while using windows console via c# programm

查看:40
本文介绍了为什么我不能通过c#programm在使用Windows控制台时查看目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System;

使用System.Text;

使用System.Diagnostics;

使用System.IO;



公共类MyClass

{

  static void Main(string [] args){

  ProcessStartInfo psi = new ProcessStartInfo();

 流程流程=新流程();              

      process.StartInfo.FileName =" cmd.exe";     
$


  while(true){

 

  string command = Console。 ReadLine();

      

      

      process.StartInfo.Arguments =" / c" +命令;

      

      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());

  

< span style ="white-space:pre">
  process.WaitForExit();



$
if(命令。等于("退出")){break;}



}

 

 }}



$

解决方案

如果要更改当前目录,请使用
Directory.SetCurrentDirectory


更改或简单获取当前目录,
目录。 GetCurrentDirectory


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;}

}
 
 }}



解决方案

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

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


这篇关于为什么我不能通过c#programm在使用Windows控制台时查看目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆