Windows应用程序中的图像保存问题c#

Mou*_*Mou 4 c# winforms

我开发了一个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;
}
Run Code Online (Sandbox Code Playgroud)

问题是一段时间赢得应用程序投掷execption喜欢

访问路径'C:\ Windows\system32\UPS\LabelImages \'被拒绝

假设我的应用程序安装在里面c:\programfiles32然后抛出这种错误我的c#exe文件.所以告诉我保存图像文件的最佳选择是什么,因此没有用户得到异常或错误,如访问路径'C:\ Windows\system32\UPS\LabelImages \'被拒绝

寻找建议.

Gra*_*ICA 8

我建议将这些文件保存在更安全的位置,例如应用程序数据的特殊文件夹:

var safeFilePath = Path.Combine(Environment.GetFolderPath(
                       Environment.SpecialFolder.ApplicationData), @"UPS\LabelImages");
Run Code Online (Sandbox Code Playgroud)

来自Environment.SpecialFolder doc:

ApplicationData:用作当前漫游用户的特定于应用程序的数据的公共存储库的目录.

你可以轻松地使用其他位置,如你认为合适(即MyDocumentsMyPictures).

磁盘上的某些位置是尝试保存文件的不好位置,因为您可能会遇到访问错误,例如"System32","Program Files"或根C:目录等.