ASP.NET Core – How to receive a file in a web API request

When the client posts a file in a multipart/form-data request, it’s loaded into an IFormFile object. This contains file information (such as the file name) and exposes the file content as a stream. This allows you to save the file or process it however you want to. You can access the IFormFile object through Request.Form.Files: … Read more

C# – Save a list of strings to a file

The simplest way to save a list of strings to a file is to use File.WriteAllLines(). This creates (or overwrites) the specified file and writes each string on a new line. The resulting file looks like this: Note: Showing non-printable newline characters \r\n for clarity. Specifying the separator character What if you want to separate … Read more

C# – Unit testing code that does File IO

If your code does File IO, such as reading text from a file, then it’s dependent on the file system. This is an external dependency. In order to make the unit tests fast and reliable, you can mock out the external dependencies. To mock out the file system dependency, you can wrap the File IO … Read more

WinForms – How to prompt the user for a file

In a WinForms project, you can prompt the user to select a file by using the OpenFileDialog control: When you call ShowDialog(), it’ll prompt the user to select a file: When the user clicks Open, you’ll be able to get the file path they selected from the OpenFileDialog.FileName property. To use the OpenFileDialog control, drag … Read more