我开发了一个 win 应用程序,它将图像保存在我们的 exe 运行的特定文件夹中。下面是我用来保存图片的
protected string GetPath(string strFileName)
{
string strPath=System.Environment.CurrentDirectory+@"\UPS\LabelImages\";
if (!System.IO.Directory.Exists(strPath))
{
System.IO.Directory.CreateDirectory(strPath);
}
strPath = strPath + strFileName;
return strPath;
}
问题是有一段时间 win apps throw execption like
Access to path 'C:\Windows\system32\UPS\LabelImages\' denied
假设我的应用程序安装在 c:\programfiles32
中,然后在我的 c# exe 文件中抛出此类错误。所以告诉我什么是保存图像文件的最佳选择,因为没有用户得到异常或错误,如 Access to path 'C:\Windows\system32\UPS\LabelImages\' denied
寻求建议。
最佳答案
我建议将这些文件保存在更安全的位置,例如应用程序数据的特殊文件夹:
var safeFilePath = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), @"UPS\LabelImages");
来自Environment.SpecialFolder文档:
ApplicationData: The directory that serves as a common repository for application-specific data for the current roaming user.
您也可以根据需要轻松使用其他位置(即 MyDocuments
或 MyPictures
)。
磁盘上的某些位置不适合尝试保存文件,因为可能会出现您所看到的访问错误,例如“System32”、“Program Files”或根 C: 目录等。
关于c# - 来自 Windows 应用程序 c# 的图像保存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30759290/