1

I'd just like to start by saying the I am extremely new to C# and coding in general so please excuse the newbie mistakes.

I have a text document that looks something like this (inserted manually)

[VIDEO]

[!VIDEO]

And I want to be able to write some text in between these 2 "tags" without erasing them.

The current code I have to write things in the document is:

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();
        }

But as you can imagine this just fully replaces everything in the document and adds the code, and the result is something like this:

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

instead of this

[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]

Is there any way to check for these tags in specific so that I can write in between? Thank you!

Edit*: Later on I'll be adding new things to this text file, for example after the [!VIDEO] line I'll have other lines with something like EX1: 4 and multiple other lines that shouldn't be overwritten, only the part in between those tags can be changed.

TL:DR - Basically I want to be able to add text between those two tags (overwriting whatever is in between) while the rest of the text file remains unchanged

Diogo Dias
  • 21
  • 4
  • Please show [mcve]. – Sinatr Jul 07 '21 at 14:41
  • Hello and welcome to SO! `I want to be able to write some text in between these 2 "tags" without erasing them` if there's data already in-between do you want to keep that data or overwrite it? – Trevor Jul 07 '21 at 14:52
  • @zaggler Thank you for the reply! Everything in between would be overwriten – Diogo Dias Jul 07 '21 at 14:55
  • 1
    OK, if your requirement is to have the `[VIDEO]` and `[!VIDEO]` at the beginning and end and you want to overwrite everything, why not just recreate it? You can write the start tag before your loop and after the loop write the ending tag. – Trevor Jul 07 '21 at 15:02
  • @zaggler Because later on I'll be adding new things to the text file, for example after the `[!VIDEO]` Ill have something like `EX1: 4` and multiple other lines that shouldn't be overwritten, only the part in between those tags can be. Don't know if I'm being able to make myself too clear – Diogo Dias Jul 07 '21 at 15:11
  • Thanks for the clarification, these details are important in order to help you. Please update your post to include these details. If the tags don't exist, do you want to create them? – Trevor Jul 07 '21 at 15:13
  • @zaggler I'll update it straight away. No the tags should always already exist, if they don't exist then nothing should happen – Diogo Dias Jul 07 '21 at 15:18

3 Answers3

0

Assuming that your method ReadVideoFiles is called only, you could do something like this:

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();
}

Instead of inserting your input between the "tags", this code produces the complete file from scratch including the tags.

Maybe this is already sufficient for you?

mu88
  • 4,156
  • 1
  • 23
  • 47
  • That does work to make me have the tags, however it does delete the other data of the text file. I've clarified my problem a little more, but basically I want to be able to write only between those tags while the rest of the text document remain unchanged. But thank you for the reply nonetheless – Diogo Dias Jul 07 '21 at 15:27
-1

If you don't want to spend too much time on it, you can just change :

sw.WriteLine(file);

by :

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

You'll have the expected result.

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
Pepe
  • 1
  • The isn't the correct output, if the OP did this it would create a new start/end tag along with two newlines for every file. – Trevor Jul 07 '21 at 15:05
-1

You can insert your text at the index of the end tag in your list without overwriting any previous text you had in the file.

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();
flyingbyte
  • 79
  • 5