SoFunction
Updated on 2025-03-06

C# IO read and write file method encapsulation code

I won't introduce it in detail, just enter the code

/// <summary>
  /// Function: FileStream file stream reads files  /// </summary>
  /// <param name="filePath">parameters: file path</param>  /// <returns>Return value: StreamReader object</returns>  public static StreamReader ReadFileByFs(string filePath)
  {
   FileStream Fs = null;
   StreamReader Sr = null;
   try
   {
    Fs = new FileStream(filePath, , );
    Sr = new StreamReader(Fs, );
   }
   catch (IOException e)
   {
    throw e;
   }
   return Sr;
  }

Code 2:
      

 /// &lt;summary&gt;
  /// Function: FileStream file streaming  /// &lt;/summary&gt;
  /// <param name="filePath">parameters: file path</param>  /// <returns>Return value: StreamWriter object</returns>  public static StreamWriter WriteFileByFs(string filePath)
  {
   FileStream Fs = null;
   StreamWriter Sw = null;
   try
   {
    Fs = new FileStream(filePath, , );
    Sw = new StreamWriter(Fs, );
   }
   catch (IOException e)
   {
    throw e;
   }
   return Sw;
  }

The above code provides a detailed introduction to the IO reading and writing file method encapsulation, hoping to help everyone.