How to give permission to create directory with c#

308 Views Asked by At

here's my code

Directory.CreateDirectory(Directory.GetCurrentDirectory() + @"\ExtractedContent");
using (StreamWriter sw = File.CreateText(Directory.GetCurrentDirectory() + @"\ExtractedContent"))
{
    sw.WriteLine($"TITLE: {extractedTitle[0]}");
    sw.WriteLine("CONTENT:");
    sw.Write(extractedContent[0]);
}`

Error:

Unhandled exception : System.UnauthorizedAccessException: Access to the access path 'C:\Users\"directory of the program"' is denied.

1

There are 1 best solutions below

0
ShwanTheFows On BEST ANSWER

the problem was that i didn't gave the file a name

using (StreamWriter sw = File.CreateText(Directory.GetCurrentDirectory() + @"\ExtractedContent\file.txt(that's the solution)"))
{
    sw.WriteLine($"TITLE: {extractedTitle[0]}");
    sw.WriteLine("CONTENT:");
    sw.Write(extractedContent[0]);
}