C# 使用循环编辑参数

C# 使用循环编辑参数,c#,iteration,C#,Iteration,我正在尝试修改一系列4.bat文件。当我运行程序时,它会提示我输入,然后将其写入.bat文件 我从.Openwrite文件的microsoft文档中获取了下面的代码,然后添加了一些变量来指向这些文件 与复制/粘贴实际写入文本的代码不同,我在其周围放置了一个for循环,目的是更改参数,以便File.OpenWrite片段在每次迭代中都会查看不同的变量(以及不同的路径/目录)。我确认循环是有效的(如果我输入一个路径变量,它将遍历并写入该文件4次),并且file.OpenWrite每次迭代都会看到正确

我正在尝试修改一系列4.bat文件。当我运行程序时,它会提示我输入,然后将其写入.bat文件

我从.Openwrite文件的microsoft文档中获取了下面的代码,然后添加了一些变量来指向这些文件

与复制/粘贴实际写入文本的代码不同,我在其周围放置了一个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);
            }
        }

    }
}
使用系统;
使用System.IO;
使用系统文本;
课堂测试
{
公共静态void Main()
{
字符串路径=System.IO.path.GetDirectoryName(System.Reflection.Assembly.GetExecutionGassembly().Location);
字符串path0=path+@“\down\u fa.bat”;
字符串path1=path+@“\down_ng.bat”;
字符串path2=path+@“\down_os.bat”;
字符串path3=path+@“\down\u sp.bat”;
字符串portinput=Console.ReadLine();
字符串dotbatinput=“DDL-p”+端口输入;
//打开流并写入它。
对于(int i=0;i<4;i++)
{
使用(FileStream fs=File.OpenWrite(“路径”+i))
{
字节[]信息=
新的UTF8Encoding(true).GetBytes(dotbatinput);
//向文件中添加一些信息。
fs.Write(信息,0,信息长度);
}
}
}
}
左侧打开一个名为“path0”的文件流,您将在项目的
bin\Debug
目录中找到该文件,右侧示例在字符串
path0
中指定的位置写入一个文件。当然,其他数字也是如此。一种可能的解决方案是使用数组或列表:

string[] paths = new string[4].Select(x => System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)).ToArray();
string[0] += ...;
string[1] += ...;
string[2] += ...;
string[3] += ...;

foreach (string path in paths)
{
    using (FileStream fs = File.OpenWrite(path))
    {
        // do stuff
    }
}

不能使用字符串和串联数字引用代码中声明的变量。通过这种方式,您可以向OpenWrite方法传递一个文本字符串,而不是名称等于字符串的变量的内容

一种更简单的方法是将每个批处理文件添加到字符串列表中,然后在该列表上循环写入所需的内容

string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
List<string> batFiles = new List<string>();
batFiles.Add(System.IO.Path.Combine(path, "down_fa.bat"));
batFiles.Add(System.IO.Path.Combine(path, "down_ng.bat"));
batFiles.Add(System.IO.Path.Combine(path, "down_os.bat"));
batFiles.Add(System.IO.Path.Combine(path, "down_sp.bat"));
string portinput = Console.ReadLine();
string dotbatinput = "DDL -p" + portinput;

foreach(string batFile in batFiles)
{
    using (FileStream fs = File.OpenWrite(batFile))
    {
     -----
    }
}
string path=System.IO.path.GetDirectoryName(System.Reflection.Assembly.getExecutionGassembly().Location);
List batFiles=新列表();
Add(System.IO.Path.Combine(路径,“down_fa.bat”);
Add(System.IO.Path.Combine(路径,“down_ng.bat”);
添加(System.IO.Path.Combine(路径,“down_os.bat”);
添加(System.IO.Path.Combine(路径,“down_sp.bat”);
字符串portinput=Console.ReadLine();
字符串dotbatinput=“DDL-p”+端口输入;
foreach(batFiles中的字符串batFile)
{
使用(FileStream fs=File.OpenWrite(batFile))
{
-----
}
}

您的猜测是正确的,只需将所有路径放在一个数组中,然后循环通过该数组即可。
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
List<string> batFiles = new List<string>();
batFiles.Add(System.IO.Path.Combine(path, "down_fa.bat"));
batFiles.Add(System.IO.Path.Combine(path, "down_ng.bat"));
batFiles.Add(System.IO.Path.Combine(path, "down_os.bat"));
batFiles.Add(System.IO.Path.Combine(path, "down_sp.bat"));
string portinput = Console.ReadLine();
string dotbatinput = "DDL -p" + portinput;

foreach(string batFile in batFiles)
{
    using (FileStream fs = File.OpenWrite(batFile))
    {
     -----
    }
}