【发布时间】:2013-01-30 19:15:05
【问题描述】:
我需要用10 lines 和10FRE001 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