Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 修改StreamReader以在未找到文件时提供错误消息_C# - Fatal编程技术网

C# 修改StreamReader以在未找到文件时提供错误消息

C# 修改StreamReader以在未找到文件时提供错误消息,c#,C#,救命啊!我对C#和代码非常陌生,只是偶尔会用到,所以请温柔一点 我正在尝试创建一种“更友好”的方法来打开文本文件进行阅读,如果在试图打开文件时无法找到该文件,那么这种方法将为用户提供更友好的信息。 为此,我创建了自己的“TextFileStreamReader”类。这与StreamReader基本相同,但如果找不到文件,则会显示一些错误消息。如果成功找到该文件,我希望返回一个实例StreamReader。但我不认为我可以这么做 关于我应该如何实现我想做的事情,有什么提示吗 //Trying to

救命啊!我对C#和代码非常陌生,只是偶尔会用到,所以请温柔一点

我正在尝试创建一种“更友好”的方法来打开文本文件进行阅读,如果在试图打开文件时无法找到该文件,那么这种方法将为用户提供更友好的信息。 为此,我创建了自己的“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");

您可以这样做:

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();
//...
然后像这样称呼这个班:

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();
//...

您可以这样做:

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();
//...
然后像这样称呼这个班:

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();
//...

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

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变量之前,您显然需要检查它是否为null,但这应该满足您的要求。

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

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变量之前,您显然需要检查它是否为null,但这应该满足您的要求。

非常感谢您的帮助,Wudge。这绝对是一个进步(因为它确实有效!),但我希望能够用一条线做任何事情。看来这一定是一种方法,但我不知道该怎么做。非常感谢你的帮助,Wudge。这绝对是一个进步(因为它确实有效!),但我希望能够用一条线做任何事情。看来这一定是一种方法,但我不知道怎么做。谢谢艾伦!这正是我想要的。我惊讶地看到,您不必实例化TextFileStreamReader类的实例,但可以使用StreamReader StreamReader=TextFileStreamReader.CreateStream(@“C:\Test\TestFile.txt”)直接引用它;别担心,刺猬。可以在不实例化TextFileStreamReader类实例的情况下调用CreateStream()方法的原因是该方法被标记为静态,这意味着它不需要调用该类的实例。哈你的解释很有帮助,链接也是如此。突然,很多事情都发生了!谢谢你,艾伦!这正是我想要的。我惊讶地看到,您不必实例化TextFileStreamReader类的实例,但可以使用StreamReader StreamReader=TextFileStreamReader.CreateStream(@“C:\Test\TestFile.txt”)直接引用它;别担心,刺猬。可以在不实例化TextFileStreamReader类实例的情况下调用CreateStream()方法的原因是该方法被标记为静态,这意味着它不需要调用该类的实例。哈你的解释很有帮助,链接也是如此。突然,很多事情都发生了!