c# - 来自 Windows 应用程序 c# 的图像保存问题

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

问题是有一段时间 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.

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

磁盘上的某些位置不适合尝试保存文件,因为可能会出现您所看到的访问错误,例如“System32”、“Program Files”或根 C: 目录等。

关于c# - 来自 Windows 应用程序 c# 的图像保存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30759290/

相关文章:

c# - 如何使用 Interface Builder 在 UIButton 或 UIBarButtonItem 上添加 BgImage?

c# - 在 Windows 8 网格中处理滑动手势

c# - Winforms绑定(bind)问题

c# - 当 DataSource 是 BindingList 时过滤 BindingSource

c# - 如何自动结束 DataGridView 单元格编辑模式?

c# - .NET 中的日期时间溢出

序列化为 XML 时 C# 控制元素名称

c# - 流程布局面板。自定义滚动条

c# - 在 WinForm 上单击按钮后设置多个文本框可编辑

winforms - 在Visual Basic Windows窗体中包含媒体