如果找不到文件,修改StreamReader以提供错误消息

时间:2016-04-12 10:33:38

标签: c#

帮助!我对C#非常陌生并偶尔编码,所以请保持温和。

我正在尝试创建一个打开文本文件进行阅读的“kinder”方法 - 如果在尝试打开文件时无法找到该文件,将为用户提供更友好的信息。 为此,我正在创建自己的“TextFileStreamReader”类。这与StreamReader基本相同,但如果找不到该文件则会显示一些错误消息。如果成功找到该文件,我想要返回一个实例StreamReader。但我认为我不允许这样做。

关于我应该如何实现我想做的任何暗示?

//Trying to create a gentler class to a text file for reading
public class TextFileStreamReader : StreamReader
{
    public static TextFileStreamReader(string fullfilename) : base(string)
    {
        try
        {
            StreamReader reader = new StreamReader(fullfilename);
            Console.WriteLine("File {0} successfully opened.", fullfilename);
            return reader;  //Can't do this - but how do I return a StreamReader?
        }
        catch (FileNotFoundException)
        {
            Console.Error.WriteLine(
            "Can not find file {0}.", fullfilename);
        }
        catch (DirectoryNotFoundException)
        {
            Console.Error.WriteLine(
            "Invalid directory in the file path.");
        }
        catch (IOException)
        {
            Console.Error.WriteLine(
            "Can not open the file {0}", fullfilename);
        }
    }
}

所需用法

TextFileStreamReader myreader = New TextFileStreamReader("C:\Test\TestFile.txt");

2 个答案:

答案 0 :(得分:0)

你可以这样做:

public class TextFileStreamReader
{
    private string _FullFileName;

    public TextFileStreamReader(string fullfilename)
    {
        _FullFileName = fullfilename;
    }

    public StreamReader GetStream()
    {
        try
        {
            StreamReader reader = new StreamReader(_FullFileName);
            Console.WriteLine("File {0} successfully opened.", _FullFileName);
            return reader;  //Can't do this - but how do I return a StreamReader?
        }
        catch (FileNotFoundException)
        {
            Console.Error.WriteLine(
            "Can not find file {0}.", _FullFileName);
        }
        catch (DirectoryNotFoundException)
        {
            Console.Error.WriteLine(
            "Invalid directory in the file path.");
        }
        catch (IOException)
        {
            Console.Error.WriteLine(
            "Can not open the file {0}", _FullFileName);
        }
        return null;
    }

}

并按照以下方式调用该类:

TextFileStreamReader tfsr = new TextFileStreamReader("fullfilename");
StreamReader sr = tfsr.GetStream();
//...

答案 1 :(得分:0)

您可以使用以下类为给定的文件路径创建一个流,该路径允许您拥有所需的用法,即使用一行调用代码创建流:

public class TextFileStreamReader
{
    /// <summary>
    /// Creates an instance of the StreamReaer class for a given file path.
    /// </summary>
    /// <param name="path">The complete file path to be read.</param>
    /// <returns>A new instance of the StreamReader class if the file was successfully read, otherwise null.</returns>
    public static StreamReader CreateStream(string path)
    {
        try
        {
            var reader = new StreamReader(path);

            Console.WriteLine(
                "File {0} successfully opened.",
                path);

            return reader;
        }
        catch (FileNotFoundException)
        {
            Console.Error.WriteLine(
                "Can not find file {0}.",
                path);
        }
        catch (DirectoryNotFoundException)
        {
            Console.Error.WriteLine(
                "Invalid directory in the file path.");
        }
        catch (IOException)
        {
            Console.Error.WriteLine(
                "Can not open the file {0}",
                path);
        }

        return null;
    }
}

然后使用它:

StreamReader streamReader = TextFileStreamReader.CreateStream(@"C:\Test\TestFile.txt");
// ...

显然需要在使用之前检查streamReader变量是否为null,但这应该满足您的要求。