【问题标题】:How to write text between certain lines using C# [duplicate]如何使用 C# 在某些行之间写入文本 [重复]
【发布时间】:2021-09-18 02:51:18
【问题描述】:

首先我想说的是,我对 C# 和一般编码非常陌生,所以请原谅新手的错误。

我有一个类似这样的文本文档(手动插入)

[VIDEO]

[!VIDEO]

而且我希望能够在这两个“标签”之间写一些文本而不擦除它们。

目前我要在文档中写东西的代码是:

private void ReadVideoFiles()
        {
            string[] files =Directory.GetFiles(@"Z:\APP_LANX\VIDEOS", "*.mp4"); //Goes to the specific location of the file and gets the whole location
            videoID = 0;

            for (int i = 0; i < files.Length; i++)
            {
                videoID++;
                files[i] = videoID.ToString() + ": " + files[i]; //changes the previous string so that it now contains the ID and the location
            }
            
            foreach (string file in files)
            {
                sw.WriteLine(file); //Uses the StreamWriter, declared previously
            }
            sw.Close();
        }

但正如你可以想象的那样,这只是完全替换了文档中的所有内容并添加了代码,结果是这样的:

1: Z:\APP_LANX\VIDEOS\vid1.mp4
2: Z:\APP_LANX\VIDEOS\vid2.mp4
3: Z:\APP_LANX\VIDEOS\vid3.mp4
4: Z:\APP_LANX\VIDEOS\vid4.mp4

而不是这个

[VIDEO]

1: Z:\APP_LANX\VIDEOS\vid1.mp4
2: Z:\APP_LANX\VIDEOS\vid2.mp4
3: Z:\APP_LANX\VIDEOS\vid3.mp4
4: Z:\APP_LANX\VIDEOS\vid4.mp4

[!VIDEO]

有什么方法可以检查这些标签,以便我可以在两者之间进行写入?谢谢!

编辑*:稍后我将在此文本文件中添加新内容,例如在 [!VIDEO] 行之后,我将添加其他行,例如 EX1: 4 和其他多个不应被覆盖的行,只有这些标签之间的部分可以更改。

TL:DR - 基本上我希望能够在这两个标签之间添加文本(覆盖中间的任何内容),而文本文件的其余部分保持不变

【问题讨论】:

  • 您好,欢迎来到 SO! I want to be able to write some text in between these 2 "tags" without erasing them 如果中间已经有数据,您要保留该数据还是覆盖它?
  • @zaggler 感谢您的回复!两者之间的所有内容都将被覆盖
  • 好的,如果您的要求是在开头和结尾都有[VIDEO][!VIDEO],并且您想覆盖所有内容,为什么不重新创建它呢?您可以在循环之前编写开始标记,在循环之后编写结束标记。
  • @zaggler 因为稍后我将在文本文件中添加新内容,例如在[!VIDEO] 之后会有类似EX1: 4 的内容以及不应覆盖的其他多行,只有那些标签之间的部分可以。不知道能不能说的太清楚了

标签: c# visual-studio text


【解决方案1】:

您可以在列表中结束标记的索引处插入文本,而不会覆盖文件中之前的任何文本。

using System.IO;
using System.Linq;

endTag = "[!VIDEO]"
foreach (string file in files)
{
     sw.Insert(sw.IndexOf(endTag), "text you want to add");
     // You can replace "text you want to add" with your file variable
}
     sw.Close();

【讨论】:

  • 它说 StreamWriter 不能使用 .Insert() 。有什么我可以改变的吗?
  • StreamWriter 类中没有 Insert() 方法。这个答案与现实无关。
  • 确保您是using System.IOusing System.Linq。看看是否有帮助。抱歉,我忘了提。
【解决方案2】:

假设您的方法 ReadVideoFiles 仅被调用,您可以执行以下操作:

private void ReadVideoFiles()
{
    string[] files =Directory.GetFiles(@"Z:\APP_LANX\VIDEOS", "*.mp4"); //Goes to the specific location of the file and gets the whole location
    videoID = 0;

    for (int i = 0; i < files.Length; i++)
    {
        videoID++;
        files[i] = videoID.ToString() + ": " + files[i]; //changes the previous string so that it now contains the ID and the location
    }
    
    // Add header to file
    sw.WriteLine($"[VIDEO]{Environment.NewLine}{Environment.NewLine}");

    foreach (string file in files)
    {
        sw.WriteLine(file); //Uses the StreamWriter, declared previously
    }

    // Add footer to file
    sw.WriteLine($"{Environment.NewLine}{Environment.NewLine}[!VIDEO]");
    
    sw.Close();
}

这段代码不是在“标签”之间插入您的输入,而是从头开始生成完整的文件,包括标签。

也许这对您来说已经足够了? ?

【讨论】:

  • 这确实可以让我拥有标签,但是它确实删除了文本文件的其他数据。我已经进一步澄清了我的问题,但基本上我希望能够只在这些标签之间写入,而文本文档的其余部分保持不变。不过还是谢谢你的回复
【解决方案3】:

如果你不想花太多时间在上面,你可以改变:

sw.WriteLine(file);

作者:

 sw.WriteLine("[VIDEO]\n\n" + file + "\n\n[!VIDEO]");

你会得到预期的结果。

【讨论】:

  • 这不是正确的输出,如果 OP 这样做,它将为每个文件创建一个新的开始/结束标记以及两个换行符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
  • 1970-01-01
  • 2016-12-23
  • 2019-10-01
  • 2020-03-17
相关资源
最近更新 更多