我正在尝试更改一系列4个.bat文件。当我运行该程序时,它会提示我输入,然后将其写入.bat文件。用循环编辑参数
我从File.Openwrite的微软文档下面的代码,然后添加一些变量指向文件。
与复制/粘贴实际写入文本的代码相反,我在其周围放置了一个for循环,目的是改变参数,以便File.OpenWrite片断将查找不同的变量(并且不同的变量路径/目录)。我证实了循环的作用(如果我输入其中一个路径#变量,它将遍历并向该文件写入4次),并且File.OpenWrite每次迭代都会看到正确的文本。我唯一的猜测是,它正在逐字查看'path#'参数,并没有将其视为变量。有人能帮助我理解我如何通过迭代改变这个论点吗?
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string path0 = path + @"\down_fa.bat";
string path1 = path + @"\down_ng.bat";
string path2 = path + @"\down_os.bat";
string path3 = path + @"\down_sp.bat";
string portinput = Console.ReadLine();
string dotbatinput = "DDL -p" + portinput;
// Open the stream and write to it.
for (int i = 0; i < 4; i++)
{
using (FileStream fs = File.OpenWrite("path" + i))
{
Byte[] info =
new UTF8Encoding(true).GetBytes(dotbatinput);
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}
}
}
你的猜测是正确的 – Steve
只要把一个数组和循环所有走过的路径相反。 –