2016-04-12 32 views
1

Help!私はC#とコードを作成することは時折ありますので、とても慣れていません。ファイルが見つからない場合にStreamReaderを変更してエラーメッセージを表示する

私は、読み込み用のテキストファイルを開くための "キンダー"方法を作成しようとしています。ファイルを開こうとしたときにファイルを見つけることができない場合、ユーザーに少しわかりやすい情報を与えます。 これを行うには、自分の "TextFileStreamReader"クラスを作成しています。これは本質的にStreamReaderと同じですが、ファイルが見つからない場合はエラーメッセージを表示します。ファイルが正常に見つかった場合は、インスタンスStreamReaderを返します。しかし、私はこれをすることが許されているとは思わない。

私がしたいことを達成するためにどうすればいいのかについてのヒントはありますか?

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

} 

と、次のようにクラスを呼び出す:あなたはこのようにそれを行うことができます

//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"); 

答えて

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であることを確認しますが、これは要件を満たす必要があります。

+0

ありがとうアラン!それはまさに私が望むものです。 TextFileStreamReaderクラスのインスタンスをインスタンス化する必要はなく、StreamReaderで直接参照できることがわかりました。streamReader = TextFileStreamReader.CreateStream(@ "C:\ Test \ TestFile.txt"); – HedgePig

+0

心配する必要はありません。 TextFileStreamReaderクラスのインスタンスをインスタンス化せずにCreateStream()メソッドを呼び出すことができるのは、メソッドが静的としてマークされているためです。つまり、呼び出されるクラスのインスタンスは必要ありません。 [MSDNリンク](https://msdn.microsoft.com/en-us/library/aa645766%28v=vs.71%29.aspx) –

+0

Ha!あなたの説明はリンクと同様に非常に役立ちます。突然、たくさんのことが起こります! – HedgePig

0

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

大変ありがとうございました。それは間違いなく改善です(実際に働くからです!)が、私はすべてを1行でやりたいと思っています。それは、これを行う方法でなければならないと思われるが、私はどのように考えることができない。 – HedgePig

関連する問題