Access to the path denied error in C#

You are trying to create a FileStream object for a directory (folder). Specify a file name (e.g. @”D:\test.txt”) and the error will go away.

By the way, I would suggest that you use the StreamWriter constructor that takes an Encoding as its second parameter, because otherwise you might be in for an unpleasant surprise when trying to read the saved file later (using StreamReader).

Leave a Comment