【问题标题】:Counting amount of copied files in C#在 C# 中计算复制文件的数量
【发布时间】:2016-01-18 11:24:03
【问题描述】:

我有一个要将文件复制到的计算机列表。

我正在尝试创建一个标签,显示当时正在复制到哪台计算机。例如:“Copying to computer1... (x of 10)”,其中 10 基于字符串数组中的行数:

var lineCount = File.ReadLines(complist).Count();

如何在每次复制新文件时更改第一个数字 (x)? (10 个中的 1 个)、(10 个中的 2 个)等...

这是我的标签的样子:

label2.Text = ("Copying to " + @computer + "... ( of " + lineCount + ")");

编辑:这是我的复制操作。这些文件被复制到每个远程系统。

        string complist = openFileDialog2.FileName;
        string patch = textBox2.Text;
        string fileName = patch;
        string patchfile = Path.GetFileName(fileName);
        var lineCount = File.ReadLines(complist).Count();

        foreach (string line in File.ReadLines(complist))
        {
            //Checks if C:\PatchUpdates exists on remote machine. If not, a folder is created.
            if (!Directory.Exists(@"\\" + line + @"\C$\PatchUpdates"))
            {
                Directory.CreateDirectory(@"\\" + line + @"\C$\PatchUpdates");
            }

            //XCOPY patch to every computer
            System.Diagnostics.Process processCopy = new System.Diagnostics.Process();
            ProcessStartInfo StartInfo = new ProcessStartInfo();
            StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            StartInfo.FileName = "cmd";
            StartInfo.Arguments = string.Format("/c xcopy " + patch + " " + @"\\{0}\C$\PatchUpdates /K /Y", line);
            processCopy.StartInfo = StartInfo;
            processCopy.Start();
            label2.Text = ("Copying to " + @line + "... (" + @num + " of " + @lineCount + ")");
            processCopy.WaitForExit();

【问题讨论】:

  • 你能显示循环文件并进行复制的代码吗?
  • 设置一个计数器,在循环开始内增加
  • 已编辑以显示文件的复制。

标签: c#


【解决方案1】:

只需将num 声明为变量并在循环中递增:

int num = 0;

foreach (string line in File.ReadLines(complist))
{
    num++;

    //...
    label2.Text = ("Copying to " + line + "... (" + num + " of " + lineCount + ")"); 
    //..
}  

顺便说一句,您正在两次读取文件中的行。一旦你得到了行数,另一次得到行本身。

如果您有一个大文件并且不想一次读取所有行(出于内存问题),使用ReadLines 会更好。但是,如果文件相对较小,那么您可以改为使用 ReadAllLines 读取文件行一次并将它们存储在如下变量中:

var lines = File.ReadAllLines(complist);

var lineCount = lines.Length;

int num = 0;

foreach (string line in lines)
{
    num++;
    //...
}

【讨论】:

  • 谢谢。当我阅读计算机列表两次时,我接受了关于 foreach 循环的建议。甚至没有意识到我正在这样做。
【解决方案2】:

只需保留一个索引,每次转到下一行时都会增加。

int num = 0;
foreach (string line in File.ReadLines(complist))
{
  ++num;
  string progress = string.Format("Copying to {0} ({1} of {2})", line, num, lineCount);
  // ...

尽管我强烈建议您使用比 linenum 更好的名称(例如 computerNamecomputerIndex),但我保留了您的变量名称。

或者,您可以将foreach 替换为老式的for 循环,这基本上颠倒了整个事情:

var lines = File.ReadLines(complist);
for (int num = 0; num < lineCount; ++num)
{
  string line = lines[num];
  string progress = string.Format("Copying to {0} ({1} of {2})", line, num, lineCount);
  // ...

【讨论】:

  • 我肯定会更改变量名称。再次阅读代码时,我只是让自己感到困惑。谢谢
【解决方案3】:
for(int i = 0; i < lineCount; i ++)
{
    label2.Text = string.Format(@"Copying to {0} ({1} of {2})", complist[i], i + 1, lineCount;
    // Copy logic ... 
}

【讨论】:

    猜你喜欢
    • 2016-04-17
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2022-01-22
    • 2010-10-18
    • 1970-01-01
    • 2012-12-11
    相关资源
    最近更新 更多