Search code examples
c#windowsknown-folders

How to give permission to create directory with c#


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.


Solution

  • 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]);
    }