【发布时间】:2021-12-11 12:17:56
【问题描述】:
所以我正在编写一个 C# 程序,它将几个文本文件组合成一个并将它们保存为一个组合文本文件。我拥有的一个问题,我有一个文本字段,它选择了保存编译的接收的预期文件夹,但是在选择所需的文件夹时,它会在文本框中生成文件名,每次都会删除最终/必须删除的文件名。保存功能正常工作。我想知道,如何删除文件目录中最后一个 / 之前最后一个字母之后的所有文本?
代码如下:
private void RecieptDisplayed_TextChanged(object sender, EventArgs e)
{
try
{
string[] fileAry = Directory.GetFiles(RecieptSelect.Text);
string input = RecieptSelect.Text;
int index = input.LastIndexOf("/");
if (index >= 0)
input = input.Substring(0, index);
MessageBox.Show("Reciepts being processed : " + index);
using (TextWriter tw = new StreamWriter(savefileas.Text + "RecieptsCombined.txt", true))
{
foreach (string filePath in fileAry)
{
using (TextReader tr = new StreamReader(filePath))
{
tw.WriteLine("Reciept for: " + " " + filePath + tr.ReadToEnd()) ;
tr.Close();
tr.Dispose();
}
MessageBox.Show("File Processed : " + filePath);
}
tw.Close();
tw.Dispose();
}
}
【问题讨论】:
标签: c# arrays string file-io textwriter