Save Stream As File In C#

In this blog, you will learn how to save the stream as a file in c#.

To achieve this, we can use the following namespace: "System.IO". Here is a custom code snippet, where the first parameter is the filePath, the second parameter is inputStream and the last parameter is fileName. filePath parameter will use for directory path where you want to save the file, inputStream will holds file stream and fileName will be your file name.
  1. public static void SaveStreamAsFile(string filePath, Stream inputStream, string fileName) {  
  2.  DirectoryInfo info = new DirectoryInfo(filePath);  
  3.  if (!info.Exists) {  
  4.   info.Create();  
  5.  }  
  6.   
  7.  string path = Path.Combine(filePath, fileName);  
  8.  using(FileStream outputFileStream = new FileStream(path, FileMode.Create)) {  
  9.   inputStream.CopyTo(outputFileStream);  
  10.  }  
  11. } 
Here is a detailed tutorial: Working with DirectoryInfo In C#