我试图将文件保存在我的项目bin文件夹中。出于某种原因,当我给它一个字符串作为路径,如
GROUP_CONCAT(DISTINCT patient_id) AS patient_ids
它可以保存文件。但是,当我尝试将其保存为
时string filePath = @"C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\ErrorLog";
Thread.Sleep(100);
bitmap.Save(filePath+@"\ErrorImage.Bmp", ImageFormat.Bmp);
我收到运行时错误string filePath = System.IO.Directory.GetCurrentDirectory() + @"\ErrorLog";
Thread.Sleep(100);
bitmap.Save(filePath+@"\ErrorImage.Bmp", ImageFormat.Bmp);
不确定为什么会这样。最初我认为它可能是文件夹权限,但是这似乎并非如此,因为它使用第一种方法。
为什么会发生这种情况?
我的代码如下
A generic error occurred in GDI+
答案 0 :(得分:3)
使用Bitmap
的Save方法时,应确保该目录存在。在第一种情况下,您的目录是:
此目录应存在于您的系统中
C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\ErrorLog
但在第二种情况下(使用Directory.GetCurrentDirectory
方法时),您的目录应该是这样的(它可能在Debug
之前有一个外部Release
或ErrorLog
文件夹)
您的系统中不应存在这些目录(取决于您处于调试或发布模式)
C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\Debug\ErrorLog
C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\Release\ErrorLog
因此bitmap.Save
会引发错误,因为您的系统中不存在该目录。