You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅在含指定内容的_PostTicket目录生成_newPostTicket并交换文件名?

仅在包含指定内容的目录下生成文件并交换文件名(C#)

我是编程新手,当前需要交换两个TXT文件的文件名:_newPostTicket与_PostTicket。但现在运行代码后,即使_PostTicket里没有getFromLine[10] == "0"的内容,也会生成_newPostTicket并执行交换。希望实现仅在包含指定内容的目录下完成生成新文件和文件名交换的操作。

示例_PostTicket文件内容

# start time = 2021-12-03-15-14-55

# end time = 2021-12-03-15-15-41

# name = SYSTEM

bot 10 pad 11 d 4 e 6 t #0 0 2021-12-03-15-14-55 # - 2021-12-03-15-15-41

bot 11 pad 12 d 5 e 7 t #0 0 2021-12-03-15-14-55 # - 2021-12-03-15-15-41

bot 12 pad 13 d 6 e 8 t #0 1 2021-12-03-15-14-55 # - 2021-12-03-15-15-41

and more

现有C#代码

DirectoryInfo srcDir = new DirectoryInfo(srcDataFolderPath);
string srcDataFolderPath = Path.Combine(DataSource, folderName);
string PostTicketPath = srcDataFolderPath + _POSTTICKET;
string newPostTicketPath = srcDataFolderPath + _newPOSTTICKET;
string tempPostTicketPath = srcDataFolderPath + _tempPOSTTICKET;


if (srcDir.Exists == true)
{
    if ((File.Exists(PostTicketPath)) &&(File.Exists(srcDataFolderPath + _PRETICKET)))
    {
        using (StreamWriter sr = new StreamWriter(newPostTicketPath))
        {
            foreach (string line in File.ReadLines(PostTicketPath))
            {
                string[] getFromLine = line.Split(' ');
                if (getFromLine.Length >= 11 && getFromLine[0] != "#" && getFromLine[10] == "0")
                {
                    getFromLine[10] = "3";
                    sr.WriteLine(String.Join(" ", getFromLine));
                }
                else
                {
                    sr.WriteLine(line);
                }
            }
        }
    }
    File.Move(newPostTicketPath, tempPostTicketPath);
    File.Move(PostTicketPath, newPostTicketPath);
    File.Move(tempPostTicketPath, PostTicketPath);
}

优化方案

核心思路

先检查_PostTicket中是否存在符合getFromLine[10] == "0"的内容,只有确认存在后,才执行生成新文件和文件名交换操作。

优化后的代码

// 修正变量声明顺序,统一用Path.Combine拼接路径避免错误
string srcDataFolderPath = Path.Combine(DataSource, folderName);
DirectoryInfo srcDir = new DirectoryInfo(srcDataFolderPath);
string PostTicketPath = Path.Combine(srcDataFolderPath, _POSTTICKET);
string newPostTicketPath = Path.Combine(srcDataFolderPath, _newPOSTTICKET);
string tempPostTicketPath = Path.Combine(srcDataFolderPath, _tempPOSTTICKET);
string preTicketPath = Path.Combine(srcDataFolderPath, _PRETICKET);

if (srcDir.Exists)
{
    if (File.Exists(PostTicketPath) && File.Exists(preTicketPath))
    {
        // 第一步:检查文件中是否存在目标内容
        bool hasTargetContent = false;
        foreach (string line in File.ReadLines(PostTicketPath))
        {
            // 分割时去除空元素,避免多余空格导致数组长度判断错误
            string[] getFromLine = line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            if (getFromLine.Length >= 11 && getFromLine[0] != "#" && getFromLine[10] == "0")
            {
                hasTargetContent = true;
                break; // 找到目标内容后立即终止遍历,提升效率
            }
        }

        // 仅存在目标内容时,执行生成和交换操作
        if (hasTargetContent)
        {
            // 生成修改后的_newPostTicket文件
            using (StreamWriter sr = new StreamWriter(newPostTicketPath))
            {
                foreach (string line in File.ReadLines(PostTicketPath))
                {
                    string[] getFromLine = line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                    if (getFromLine.Length >= 11 && getFromLine[0] != "#" && getFromLine[10] == "0")
                    {
                        getFromLine[10] = "3";
                        sr.WriteLine(string.Join(" ", getFromLine));
                    }
                    else
                    {
                        sr.WriteLine(line);
                    }
                }
            }

            // 执行文件名交换
            File.Move(newPostTicketPath, tempPostTicketPath);
            File.Move(PostTicketPath, newPostTicketPath);
            File.Move(tempPostTicketPath, PostTicketPath);
        }
    }
}

关键优化点

  1. 修正路径问题:调整变量声明顺序,统一使用Path.Combine拼接路径,避免因手动拼接分隔符导致的路径错误。
  2. 新增内容校验:先遍历文件一次确认目标内容存在,再执行后续操作,避免无意义的文件生成和交换。
  3. 优化分割逻辑:使用StringSplitOptions.RemoveEmptyEntries处理行内多余空格,防止分割后数组长度判断失效。
  4. 提升执行效率:找到目标内容后立即终止遍历,减少不必要的IO操作。

内容的提问来源于stack exchange,提问作者Yzj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 15:40:50