Save Stream As 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.

public static void SaveStreamAsFile(string filePath, Stream inputStream, string fileName) {  
 DirectoryInfo info = new DirectoryInfo(filePath);  
 if (!info.Exists) {  
  info.Create();  
 }  
  
 string path = Path.Combine(filePath, fileName);  
 using(FileStream outputFileStream = new FileStream(path, FileMode.Create)) {  
  inputStream.CopyTo(outputFileStream);  
 }  
}

Here is a detailed tutorial: Working with DirectoryInfo In C#