Can't save files on the desktop

Деян Цонев 1 Reputation point


Recently I have been struggling with an issue in my Windows Forms application, written in C#. In brief, the program generates an HTML document and a few images (png) that need to be saved as a folder chosen by the user via the FolderBrowserDialog.

Whenever I try to save my folder on the Desktop, My Documents or any other location on disk C:, I observe the following exception:

System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+.'. I have tried a workaround , copying byte array from memory stream to file stream, but then the error is : System.UnauthorizedAccessException: Access to the path 'C:\Users\DidoPc\Desktop\djdfjdfjdfj folder from February 07, 2021\QRUSR.Png' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath).

Even when manually adding full permissions to the directory where I want to save the files, I still don't have any access from the point of view of the program.

I have no problem saving to drive D:, but that's not the point... The excepted behaviour for drive C: should be the same as well.

I will be grateful if anyone can help with some suggestions.


FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)

                PathToExportedHtml.Text = folderBrowserDialog.SelectedPath;

                string folderPath = folderBrowserDialog.SelectedPath;

  string location = folderPath + $"\\{usernameTextBox.Text}   folder from {DateTime.Now.ToString("MMMM dd, yyyy")}";
                Debug.WriteLine("Selected path is: " + folderPath);
                Debug.WriteLine("Location after concatenation is: " + location);

                DirectoryInfo dInfo = Directory.CreateDirectory(location);
                DirectorySecurity dSecurity = dInfo.GetAccessControl();

dSecurity.AddAccessRule(new FileSystemAccessRule(new         SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
                Directory.SetAccessControl(location, dSecurity);

                Common.pathToCreatedReport = location + $"\\{usernameTextBox.Text}_Report({DateTime.Now.ToString("MMMM dd, yyyy")}).html";

                       if (Username_Pass_QR != null)
                        thereIsUsrNameQr = true;
                        var usernamePassImage = accountCredQrCode.Image;

                        var usernamePassImageBitmap = new Bitmap(usernamePassImage);

                        byte[] usernamePassImageBitmapAsByteArray = 


                        MemoryStream usernamePassImageAsMemStream = new MemoryStream(usernamePassImageBitmapAsByteArray, true);

                            using (FileStream file = new FileStream(Path.Combine(location, "QRUSR.Png"), FileMode.Create, System.IO.FileAccess.Write))

                    if (SSID_PSK_QR != null)
                        thereIsSSIDQr = true;
                        var ssidImg = wifiCredQrCode.Image;  

                        var ssidImageBitmap = new Bitmap(ssidImg);

                        ssidImageBitmap.Save(location + "\\QRSSID.Png", ImageFormat.Png);

                    ReportComponents components = new ReportComponents(SSID, PSK, HiddenWpa, Username, Password, thereIsUsrNameQr, thereIsSSIDQr);
                    Report report = new Report();


                    Bitmap logoImg = new Bitmap(Properties.Resources.GroupLogo___Adaptive_Icon);
                    Bitmap emptyQr = new Bitmap(Properties.Resources.EmptyQr);
                    emptyQr.Save(location + "\\EmptyQr.jpg", ImageFormat.Png);
                    logoImg.Save(location + "\\GroupLogo - Adaptive Icon.png", ImageFormat.Png);

                    MessageBox.Show("Export of report complete successfully!");
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,813 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Ken Tucker 5,846 Reputation points

    You need to use the filesavedialog to be to get permission to save to a desktop

    1 person found this answer helpful.
    0 comments No comments

  2. Daniel Zhang-MSFT 9,611 Reputation points

    Hi 49159189,
    First, please make sure that the saved folder has read/write permissions.
    Then try the following code:

    using (var ms = new MemoryStream(‘yourImageByte’)) {  
        patternImage = new Bitmap(ms);  
        patternImage.Save("filePath", ImageFormat.Jpeg);  

    Best Regards,
    Daniel Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Karen Payne MVP 35,026 Reputation points

    Try adding a manifest file

    1. Right click on the project in Solution Explorer
    2. Add new item
    3. Search for manifest
    4. Once added replace the contents with the following.

    Does that help or not ?

    <?xml version="1.0" encoding="utf-8"?>
    <assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
      <assemblyIdentity version="" name=""/>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
          <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
      <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    0 comments No comments