2014-02-27 50 views
0

我试图让一个用户输入一个文件路径作为一个字符串(但为了学习的目的,我想把任何字符串变量的路径,但我打开到文件路径验证的建议)。我有和似乎无法弄清楚的问题是一旦inFilepath从Console.ReadLine()更新,它只会显示在连接它的Console.WriteLine()语句。试图在任何地方显示“”,我知道这表明变量自从赋值为“”以来没有更新。我认为这是一个范围问题,但我为什么最终只能访问与声明相同范围内的更新后的inFilepath。验证用户输入和更新循环内的变量

string correctPath = "no"; 
string inFilepath = ""; 
bool flag = false; 
bool inFlag = false; 
while (!flag) { 
    Console.WriteLine("Please Enter the Tour file path now..."); 
    inFilepath = Console.ReadLine(); 
    inFilepath += "\\"; 
    while (!inFlag) { 
     Console.WriteLine("Is this the correct filepath: " + inFilepath + " ?" + 
     "\n Enter 'no' if it is incorrect or 'yes' if it is correct."); 
     correctPath = Console.ReadLine(); 
     if (correctPath == "yes") { 
      inFlag = true; 
      flag = true; 
     } 
     else { 
      break; 
     } 
    } 
} 
Console.WriteLine("path: ", inFilepath); 

回答

1

你的问题是在这里,你是路过帕拉姆ARG,但不使用/显示它

Console.WriteLine("path: ", inFilepath); 

您需要添加{0},像

Console.WriteLine("path: {0}", inFilepath);