我开发了一个Windows应用程序,可以将图片保存在特定的文件夹中,这个文件夹是我们的.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;
}
问题在于有时候Windows应用程序会抛出异常,如下所示:
假设我的应用程序安装在Access to path 'C:\Windows\system32\UPS\LabelImages\' denied
c:\programfiles32
中,然后我的C# exe文件会抛出这种错误。那么告诉我什么是保存图像文件的最佳选项,以便没有用户会出现异常或错误,如Access to path 'C:\Windows\system32\UPS\LabelImages\' denied。寻求建议。
CreateDirectory
会为您完成这项工作,因此您可以每次都调用它。 - Sami Kuhmonen