C# 环路内的IO操作
我正在编写一个小型控制台应用程序,它必须用另一个txt文件覆盖一个txt文件,但是最终执行3次,我认为这是因为IO写入过程比IO输出过程慢。任何人都可以帮助我如何只执行一次循环 代码如下:C# 环路内的IO操作,c#,io,console-application,C#,Io,Console Application,我正在编写一个小型控制台应用程序,它必须用另一个txt文件覆盖一个txt文件,但是最终执行3次,我认为这是因为IO写入过程比IO输出过程慢。任何人都可以帮助我如何只执行一次循环 代码如下: while (confirm != 'x') { Console.WriteLine( "Do you want to copy the archive to test2.txt? (y)es or e(x)it"); confirm = (char)Console.Read(
while (confirm != 'x') {
Console.WriteLine(
"Do you want to copy the archive to test2.txt? (y)es or e(x)it");
confirm = (char)Console.Read();
if (confirm == 's') {
File.Copy("C:\\FMUArquivos\\test.txt",
"C:\\FMUArquivos\\test2.txt", true);
Console.WriteLine("\nok\n");
}
Console.WriteLine("\ncounter: " + counter);
counter += 1;
}
请尝试以下代码:
var counter = 0;
Console.WriteLine("Do you want to copy the archive to test2.txt? (y)es or e(x)it");
var confirm = Console.ReadLine();
while (confirm != "x")
{
File.Copy("C:\\FMUArquivos\\test.txt", "C:\\FMUArquivos\\test2.txt", true);
Console.WriteLine("\nok\n");
counter += 1;
Console.WriteLine("\ncounter: " + counter);
Console.WriteLine("Do you want to copy the archive to test2.txt? (y)es or e(x)it");
confirm = Console.ReadLine();
}
它会询问您是否要继续,如果您按y
(或除x
)以外的任何键,
它将复制文件并打印“\nok\n”和“1”。然后它会再次询问您,如果您按x
,它将停止。请尝试以下代码:
var counter = 0;
Console.WriteLine("Do you want to copy the archive to test2.txt? (y)es or e(x)it");
var confirm = Console.ReadLine();
while (confirm != "x")
{
File.Copy("C:\\FMUArquivos\\test.txt", "C:\\FMUArquivos\\test2.txt", true);
Console.WriteLine("\nok\n");
counter += 1;
Console.WriteLine("\ncounter: " + counter);
Console.WriteLine("Do you want to copy the archive to test2.txt? (y)es or e(x)it");
confirm = Console.ReadLine();
}
它会询问您是否要继续,如果您按y
(或除x
)以外的任何键,
它将复制文件并打印“\nok\n”和“1”。然后它会再次询问您,如果您按x
,它将停止。如果您按y
,则这将为您提供3个字符的序列“y”
+++
,并将产生三次迭代,因此计数器将增加3。改用ReadLine
int counter = 0;
while (true) {
Console.WriteLine("Do you want to copy ...");
string choice = Console.ReadLine();
if (choice == "x") {
break;
}
if (choice == "y") {
// Copy the file
} else {
Console.WriteLine("Invalid choice!");
}
counter++;
}
如果您点击y
,则这将为您提供3个字符的序列“y”
++
,并将产生三次迭代,因此计数器将增加3。改用ReadLine
int counter = 0;
while (true) {
Console.WriteLine("Do you want to copy ...");
string choice = Console.ReadLine();
if (choice == "x") {
break;
}
if (choice == "y") {
// Copy the file
} else {
Console.WriteLine("Invalid choice!");
}
counter++;
}
现在我已经复制并运行了您的代码,我看到了您的问题。您应该将对“Read”的调用替换为“ReadLine”,并将确认类型更改为字符串,并对其进行比较
原因是Console.Read仅在您点击“回车”时返回,因此它读取3个字符;'s“”\r',“\n”(最后2个是Windows上的换行符)
有关Console的API参考,请参见此处。阅读:
试试这个
string confirm = "";
int counter = 0;
while (confirm != "x")
{
Console.WriteLine("Do you want to copy the archive to test2.txt? (y)es or e(x)it");
confirm = Console.ReadLine();
if (confirm == "s")
{
File.Copy("C:\\FMUArquivos\\test.txt",
"C:\\FMUArquivos\\test2.txt", true);
Console.WriteLine("\nok\n");
}
Console.WriteLine("\ncounter: " + counter);
counter += 1;
}
现在我已经复制并运行了您的代码,我看到了您的问题。您应该将对“Read”的调用替换为“ReadLine”,并将确认类型更改为字符串,并对其进行比较
原因是Console.Read仅在您点击“回车”时返回,因此它读取3个字符;'s“”\r',“\n”(最后2个是Windows上的换行符)
有关Console的API参考,请参见此处。阅读:
试试这个
string confirm = "";
int counter = 0;
while (confirm != "x")
{
Console.WriteLine("Do you want to copy the archive to test2.txt? (y)es or e(x)it");
confirm = Console.ReadLine();
if (confirm == "s")
{
File.Copy("C:\\FMUArquivos\\test.txt",
"C:\\FMUArquivos\\test2.txt", true);
Console.WriteLine("\nok\n");
}
Console.WriteLine("\ncounter: " + counter);
counter += 1;
}
如果只想执行一次代码,为什么要使用循环呢?循环用于重复代码…?@Kornel-“我认为这是因为IO写入过程比IO输出过程慢。”答:胡说八道。我向你保证这不是问题;)我建议在另一个线程中执行IO操作:)PS:为什么要请求“y”或“x”。。。然后检查“确认=='s'”?谢谢艾哈迈德!实际上是Console.Read()命令。是我的错,没有阅读文档!如果只想执行一次代码,为什么要使用循环呢?循环用于重复代码…?@Kornel-“我认为这是因为IO写入过程比IO输出过程慢。”答:胡说八道。我向你保证这不是问题;)我建议在另一个线程中执行IO操作:)PS:为什么要请求“y”或“x”。。。然后检查“确认=='s'”?谢谢艾哈迈德!实际上是Console.Read()命令。是我的错,没有阅读文档!