Hi, I'm a bit rusty with my C# but I'm trying to write a program for this website I am making. It's just a little something to help me edit certain texts in my website faster than what I normally do. Anyway the program I am making, I want it so that when I press a button (the file will already be chosen ahead of time) the program will take a specifically predetermined section of text and load it to a richtextbox. Then the user can edit it to their liking and press another button to save the content back into the original file.

I've been researching a bit online and I'm still having a bit of trouble. I was able to scrap pieces of code elsewhere but they were only able to read the first line of specific single lines in a text file. I don't know if C# can open / read php or html files, I'm sure they can, it's been a while since I used it, but just for extra info I want to be opening up php, html, css files, simple files such as those for websites.

This is the code I got so far, but the program keeps crashing on me.

        private void button1_Click(object sender, EventArgs e)
        {
            string index = thisapp + @"\" + "index.php";

            if (File.Exists(index))
            {
                using (var sr = new StreamReader(index))
                {
                    var textInBetween = new List<string>();
                    bool startTagFound = false;

                    while (!sr.EndOfStream)
                    {
                        var line = sr.ReadLine();
                        if (String.IsNullOrEmpty(line))
                        {
                            continue;
                        }

                        if (!startTagFound)
                        {
                            startTagFound = line.StartsWith("&bull; Administrator: Nicu L. <br />");
                            continue;
                        }

                        bool endTagFound = line.StartsWith("&bull; Website Dev: Reece D. <br />");
                        if (endTagFound)
                        {
                            richTextBox1.Text = line;
                        }
                        textInBetween.Add(line);
                    }
                }
            }
            else if (File.Exists(index) == false)
            {
                MessageBox.Show("Index.php Could Not Be Found");
            }

Thanks for anyone who can help me!!

In what way does it crash? Is there an exception you are getting? I was able to run the program without it crashing. C# can, of course, open any type of file, it's just a bunch of bytes...

Also, might I recommend you avoid using var that heavily. It has some specific uses, but is hardly necessary in this case.

Oh no I meant when I run the application in debug mode, and I hit the button the application freezes so I have to force close it. C# runs fine by all means. It's just the application when I F5 it. This other code I found was very similar and it was using String I think, but then I saw this one so I thought I'd give it a try, but to no avail :(

Try removing line 14, and changing your while loop to this:

for (string line = sr.ReadLine(); line != null; line = sr.ReadLine())

I'm on my cell at the moment, so I can't test anything right now, but it might give some weird results if the last line is empty. I'll try and remember to test it tomorrow...

Hi, I tried that out but when I hit the button nothing happened. I managed to find another code relevant to my situation. It works quite fine, but only problem is trying to cut off the information at the end because it is not needed. After that I just need to be able to edit it and have it save that information back into the file in the exact area

        private void button2_Click(object sender, EventArgs e)
        {
            string starter = "<!-- smLB Start Edit -->";
            string stopper = "<!-- smLB End Edit -->";

            string targetFile = thisapp + @"\" + "index.php";
            StreamReader readfile = new StreamReader(targetFile);
            string fileread = "";
            try
            {
                using (readfile)
                {
                    fileread = readfile.ReadToEnd();
                }
                int i = fileread.IndexOf(starter);
                string dd = fileread.Substring(i);
                richTextBox1.Text = dd;
            }
            catch (Exception)
            {
                // idk QQ
            }
        }

And thanks a lot for helping me, really appreciate it!

You would need to get the index of stopper as well, like this:

int i2 = fileread.IndexOf(stopper, i);

You pass in i so it starts searching the string from that index, which prevents it from returning an index less than i. Then you would take the difference of i2 and i, and pass it in as the length of the substring, like so:

string dd = fileread.Substring(i, i2 - i);

Also, don't forget to check for negative values of i and i2, as that indicates that the string wasn't found in IndexOf(). Another thing, this works fine for small files, but keep in mind that this method allocates an array of characters as large as the file (actually double, since each byte is represented as a char (2 bytes)).

Thanks that worked just fine. Though my stopper string wasn't part of the text, although it's not a big deal. And most my webfiles that I'm planning on editing aren't more than 1mb so far.

I don't want to both you much but if you could help me with one more thing that'd be great. I got my file to save, but I can't remember how to save back all the information back to the same, with the edited info from my richtextbox in same place. When I try to save it back, the text is all spread out to one line, and not the 10 lines or so it was originally divided into.

            string targetFile = thisapp + @"\" + "index.php";

            if (File.Exists(targetFile))
            {
                using (StreamReader srbt1 = new StreamReader(targetFile))
                {
                    srbt1.ReadToEnd();
                    srbt1.Close();
                    using (TextWriter tw = new StreamWriter(targetFile))
                    {

                        tw.Write(richTextBox1.Text, targetFile);
                        MessageBox.Show("File Saved");
                    }
                }
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.