[英]How to Delete the last line of a file.txt
I'm reading a file.txt
using StreamReader
and writing using streamWriter
. 我正在使用
StreamReader
读取file.txt
并使用streamWriter
进行streamWriter
。
I'd like to know if it's possible to ´DELETE JUST THE LAST INSERTED LINE ? i'm inserting lines, but sometimes one of these inserts are
我想知道是否有可能'刚刚插入最后的插入线
? i'm inserting lines, but sometimes one of these inserts are
? i'm inserting lines, but sometimes one of these inserts are
string.empty . Then It goes to an
? i'm inserting lines, but sometimes one of these inserts are
string.empty . Then It goes to an
. Then It goes to an
exception`that I created... In this exception, I want to delete that line I just inserted. . Then It goes to an
我创建. Then It goes to an
异常...在这个例外中,我想删除刚刚插入的那一行。
But I recreate the file, or something like that, I need just to remove/erase/delete the last line. 但我重新创建文件,或类似的东西,我只需删除/删除/删除最后一行。 May I do that ?
我可以那样做吗?
MyCode: IF you guys have another way to do this, i'd be very thankfull ! MyCode:如果你们有另一种方法可以做到这一点,我会非常感激!
using (StreamWriter streamW = new StreamWriter(fileFinal, true)) { if (contador == numeroCampos) { contador = 0; using(StreamWriter streamW = new StreamWriter(fileFinal,true)){if(contador == numeroCampos){contador = 0; }
}
foreach (string s in clb_frente_impressao.Items)
{
if (camposEscritos >= 10 * numeroCampos)
{
streamW.WriteLine();
streamW.WriteLine("-------------------------------------------------------");
streamW.Write(nomearquivo + x.ToString());
streamW.WriteLine();
x++;
camposEscritos = 0;
}
if (contador >= clb_frente_impressao.Items.Count)
{
contador = 0;
}
switch (s)
{
case "numero_carteira":
if (campos_obrigatorios.Contains("numero_carteira") && campo.numero_carteira == "")
{
dados_inexistentes++;
skipToNext = true;
break;
}
else if (campo.numero_carteira != "")
{
string aux = "";
qtdZeros = Txt_qtdZeros.Text;
if (qtdZeros == "")
{
qtdZeros = "8";
}
while (campo.numero_carteira.Length < Convert.ToInt32(qtdZeros))
{
campo.numero_carteira = campo.numero_carteira.Insert(0, "0");
}
if (usaC == "sim")
{
campos += @"\" + "*C" + aux + campo.numero_carteira + "*" + @"\";
camposEscritos++;
}
else
{
campos += @"\" + "*" + aux + campo.numero_carteira + "*" + @"\";
camposEscritos++;
}
if (contador == 0)
{
streamW.WriteLine();
streamW.Write("{0,-15}", campo.numero_carteira);
contador++;
}
else
{
streamW.Write("{0,-15}", campo.numero_carteira);
contador++;
}
}
break;
case "matricula":
if (campos_obrigatorios.Contains("matricula") && campo.matricula == "")
{
dados_inexistentes++;
skipToNext = true;
break;
}
camposEscritos++;
if (campo.matricula != "")
{
if (campo.tipo_pessoa == "3")
{
campos += @"\" + campo.matricula + "-" + campo.cod_dependente + @"\";
}
else
{
campos += @"\" + campo.matricula + @"\";
}
}
if (contador > 0)
{
if (campo.cod_dependente != "")
{
streamW.Write("{0,-10}", campo.matricula + "-" + campo.cod_dependente);
contador++;
}
else
{
streamW.Write("{0,-10}", campo.matricula);
contador++;
}
}
else
{
if (campo.cod_dependente != "")
{
streamW.WriteLine();
streamW.Write("{0,-10}", campo.matricula + "-" + campo.cod_dependente);
contador++;
}
else
{
streamW.WriteLine();
streamW.Write("{0,-10}", campo.matricula);
contador++;
}
}
break;
if (skipToNext) break;
} //Final do ForEach
if (skipToNext)
{
//HERE IS WHERE I WANT TO DELETE THE LAST LINE THAT WAS WRITED BY streamW
continue;
}
eg: When It gets into case:"numero_carteira"
and it's not empty,then it writes ok, but when I get to the matricula
AND its empty, it will break
and go to the exception I created. 例如:当它进入
case:"numero_carteira"
,它不是空的,然后将其写入确定,但是当我到达matricula
和它的空,它会break
,往我所创建的例外。 I want to delete the line there ;s Hope I could be clear ! 我想删除那里的那条线; s希望我能清楚!
If the file is not too large, you can simply use this code: 如果文件不是太大,您只需使用以下代码:
var lines = File.ReadAllLines(pathToFile);
File.WriteAllLines(pathToFile, lines.Take(lines.Length - 1));
So you have to write all lines except the last. 因此,您必须编写除最后一行以外的所有行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.