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

时间:2015-06-10 14:26:44

标签: 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;
}

问题是一段时间赢得应用程序抛出像

这样的行为
  

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

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

寻找建议。

3 个答案:

答案 0 :(得分:8)

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

var safeFilePath = Path.Combine(Environment.GetFolderPath(
                       Environment.SpecialFolder.ApplicationData), @"UPS\LabelImages");

来自Environment.SpecialFolder doc:

  

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

您也可以根据需要轻松使用其他位置(例如MyDocumentsMyPictures)。

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

答案 1 :(得分:1)

您可能需要以管理员身份运行程序。 为此,请尝试在Properties / app.manifest文件中添加以下行:

<security>
  <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
  </requestedPrivileges>

...     

答案 2 :(得分:0)

protected string GetPath(string strFileName)
{
     string strPath=Path.Combine(Environment.GetFolderPath(
        Environment.SpecialFolder.ApplicationData), @"\UPS\LabelImages\"  + strFileName);
     return strPath;
}