简体   繁体   中英

How can I open a specific text file without using OpenFileDialog?

I have a rich text editor that I have created in C#. One of the features I am now trying to add is templates. I do not want the user to have to use an OpenFileDialog to navigate to the template and open the file. I would like to specify the filepath myself so that the user only has to click one button in order to open the template.

Currently, I am trying to achieve this using the following code:

private void formalLetterToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            FileStream fileStream = new FileStream(@".\templates\tmp1.rtf", FileMode.Open);
            String str;
            str = fileStream.ToString();
            string fileContents = File.ReadAllText(filepath);
            fileContents = fileStream.ToString();
            try
            { 
                if (richTextBoxPrintCtrl1.Modified == true);
                {
                    NewFile();
                }
                richTextBoxPrintCtrl1.Rtf = fileContents;
            }
            catch (Exception exception)
            {
                MessageBox.Show("There was an error opening the template. " + exception, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        catch (Exception exception)
        {
            MessageBox.Show("There was an error opening the template. " + exception, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

However, whenever I try to open the template, I get an exception that is as follows:

System.ArgumentsException: File format is not valid.

However, I have tried to open the file using my OpenFileDialog and that works fine. Could somebody assist me in getting this working correctly?

You are making very heavy weather of loading RTF. Your code to read a file into a string will never work, as @evanmcdonnal explained. Did your file dialog based code that succeeded really do it like that? Remember that a file dialog is just UI that generates a file name in a string. If your code with a file dialog works, then it will work when the file dialog is replaced with a hard coded string.

I suspect that some part of your problem is that you are using a relative path. Perhaps the working directory is not what you expect it to be. You should specify the full path to the file.

In any case, to load RTF simply call the LoadFile method of the control. But I strongly recommend passing the full path to the file.

richTextBoxPrintCtrl1.LoadFile(fullPathToRtfFile);

Your problem is that you're trying to convert the file to a string using str = fileStream.ToString(); however, this converts the filestream to a string which is not the same thing.

Instead just do string fileContents = File.ReadAllText(filepath); to get all of the files contents into a string. You only need to use a FileStream/StreamReader if you're going to do some type of processing on the file.

Also, your use of the FileStream is a little off. I think what you really want is a StreamReader with something like this (example from msdn);

            using (StreamReader sr = new StreamReader("TestFile.txt")) 
            {
                string line;
                // Read and display lines from the file until the end of  
                // the file is reached. 
                while ((line = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(line);
                }
            }

A FileStream cannot be used to read a file. It must be passed to a StreamReader in order to actually read the file and in this case there is no point in doing that because there is an overload of the constructor which takes a filepath. It's only useful if you don't know what kind of stream the reader is going to be reading.

Where you have;

        FileStream fileStream = new FileStream(@".\templates\tmp1.rtf", FileMode.Open);
        String str;
        str = fileStream.ToString();
        string fileContents = File.ReadAllText(filepath);
        fileContents = fileStream.ToString();

You actually just want thins line; string fileContents = File.ReadAllText(filepath); , nothing else. There is no need for a FileStream when you're just reading all the text into a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM