【问题标题】:How to create consecutive named files如何创建连续的命名文件
【发布时间】:2013-01-30 19:15:05
【问题描述】:

我需要用10 lines10FRE001 then 10FRE002 之类的名称创建files.txt,知道吗?我该怎么做?如何检查是否已经有 10 行以及如何将名称从 10FRE001 增加到 10FRE002 ? 要检查是否已经有 10 行,我想使用 int counter++; 我会看到...但我不知道如何使用文件名...有什么提示吗?谢谢大家

这是我目前的代码,它读取和写入一个新的文本文件,但我不知道如何更改文件的名称......

string conteudo="";
int numeroLinhas=0;

using(var writer=new StreamWriter(Txt_DestinoPath.Text, true))
using(var reader=new StreamReader(Txt_OrigemPath.Text)) {
    while(reader.ReadLine()!=null)
        numeroLinhas++;

    string oi=numeroLinhas.ToString();

    writer.WriteLine(oi);
    int contador=0;

    for(int i=0; i<numeroLinhas; i++) {
        if(contador<9) {
            contador++;
            writer.WriteLine(@"\campo0"+contador+@"\");
        }
        else {
            contador++;
            writer.WriteLine(@"\campo"+contador+@"\");
        }
    }
}

using(var sw=new StreamWriter(Txt_DestinoPath.Text, true))
using(var sr=new StreamReader(Txt_OrigemPath.Text))
    while((conteudo=sr.ReadLine())!=null) {
        // Tira asteriscos,remove valor vazio, insere barras 
        string[] teste=conteudo.Split(new[] { '*' }, StringSplitOptions.RemoveEmptyEntries);

        var noEmptyParts=
            teste
                .Where(p => !String.IsNullOrWhiteSpace(p))
                .Skip(2)
                .Select(p => String.Format("\\{0}\\", p.Trim()))
                .ToArray<String>();

        string resultado=String.Join("", noEmptyParts);

        if(Txt_DestinoPath.Text==""||Txt_DestinoPath.Text==string.Empty) {
            MessageBox.Show("Nenhum Caminho de Origem Escolhido.");
            return;
        }

        // Cria novo arquivo txt                
        sw.WriteLine(resultado);
    }

上面计数器的结果是:\campo01\, \campo02\, \campo03\ .... \campo10\, \campo11\ ...

我需要改的名字我会放在上面

Txt_DestinoPath.Text + FileThatIneedToIncrement,true)

【问题讨论】:

  • 你已经尝试了什么?
  • 为了与 SD 中的 JG 刚才所说的相吻合,如果有代码,请发布一些代码。
  • 我创建了 2 个 StreamReader 和 2 个 StreamWriter 我试图创建一个计数器并增加它 ´counter++` 但它不适用于名称... 001 -> 002部分... =\
  • 在使用前将int 转换为string & concat 与字符串的其他部分?
  • 已编辑:我尝试了与上面所做的相同的事情。检查第一个数字是否 > 9,然后我将增加另一个数字......但我做不到。我需要更改这些文件名以将其放在 StreamWriter 的路径上......希望我能清楚

标签: c# winforms visual-studio-2010


【解决方案1】:

要做10FRE001、10FRE002、...命名你可以试试这个:

for(int i = 0; i<10; i++)
{
    string fileName = string.Format("10FRE{0:D3}",i);
    // other codes
}

看看thisthis

【讨论】:

  • 我现在就试试...你能解释一下{0:D3} 部分吗?谢谢
  • 我只是做一些测试,在这里完成后,我会将其标记为答案...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-30
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多