You need to use the filesavedialog to be to get permission to save to a desktop
Can't save files on the desktop
Hi,
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.
Best,
Deyan
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);
dInfo.SetAccessControl(dSecurity);
Common.pathToCreatedReport = location + $"\\{usernameTextBox.Text}_Report({DateTime.Now.ToString("MMMM dd, yyyy")}).html";
try
{
if (Username_Pass_QR != null)
{
thereIsUsrNameQr = true;
var usernamePassImage = accountCredQrCode.Image;
var usernamePassImageBitmap = new Bitmap(usernamePassImage);
byte[] usernamePassImageBitmapAsByteArray =
Miscalleneous.Instance.ImageToByteArray(usernamePassImageBitmap);
MemoryStream usernamePassImageAsMemStream = new MemoryStream(usernamePassImageBitmapAsByteArray, true);
using (FileStream file = new FileStream(Path.Combine(location, "QRUSR.Png"), FileMode.Create, System.IO.FileAccess.Write))
usernamePassImageAsMemStream.CopyTo(file);
}
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();
report.Generate(components);
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!");
3 answers
Sort by: Most helpful
-
-
Daniel Zhang-MSFT 9,621 Reputation points
2021-02-08T06:28:59.223+00:00 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.
-
Karen Payne MVP 35,386 Reputation points
2021-02-08T16:41:05.67+00:00 Try adding a manifest file
- Right click on the project in Solution Explorer
- Add new item
- Search for manifest
- 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="1.0.0.0" name="MyApplication.app"/> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> <security> <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo> <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> <application> </application> </compatibility> </assembly>