我在 winforms c# 中的应用程序和我的表单需要从数据库中检索图像并存储在本地机器(我的应用程序安装在哪里)并在 PictureBox 控件中查看。
我有以下代码(在本地存储图像并在 PictureBox 控件中查看):
//dsProjects is a dataset which is retrieved from database
if (dsProjects.Tables[0].Rows.Count > 0)
{
int idxCurPageJob = (iPageNum * 8);
string path = AppDomain.CurrentDomain.BaseDirectory;
string folder = "TEMP";
string fullpath = string.Empty;
fullpath = Path.Combine(path, folder);
bool exists = System.IO.Directory.Exists(fullpath);
try
{
if (!exists)
Directory.CreateDirectory(folder);
else
{
Directory.Delete(fullpath, true);
Directory.CreateDirectory(folder);
}
}
catch (Exception)
{}
try
{
if (dsProjects.Tables[0].Rows.Count > idxCurPageJob)
{
pnl1.Visible = true;
pnl1.AccessibleName = dsProjects.Tables[0].Rows[idxCurPageJob][1].ToString();
Labelfld1.Text = dsProjects.Tables[0].Rows[idxCurPageJob][0].ToString();
byte[] bgImg = (byte[])dsProjects.Tables[0].Rows[idxCurPageJob][2];
string proCode = dsProjects.Tables[0].Rows[idxCurPageJob][3].ToString();
string strfn = Path.Combine(fullpath, proCode + Convert.ToString(DateTime.Now.ToFileTime()) + idxCurPageJob);
using (FileStream fs = new FileStream(@strfn, FileMode.Create, FileAccess.Write))
{
fs.Write(bgImg, 0, bgImg.Length);
fs.Flush();
fs.Close();
}
picBox1.Image = Image.FromFile(strfn);
picBox1.SizeMode = PictureBoxSizeMode.StretchImage;
picBox1.Refresh();
}
if (dsProjects.Tables[0].Rows.Count > (idxCurPageJob + 1))
{
pnl2.Visible = true;
Labelfld2.Text = dsProjects.Tables[0].Rows[idxCurPageJob + 1][0].ToString();
pnl2.AccessibleName = dsProjects.Tables[0].Rows[idxCurPageJob + 1][1].ToString();
byte[] bgImg = (byte[])dsProjects.Tables[0].Rows[idxCurPageJob + 1][2];
string proCode = dsProjects.Tables[0].Rows[idxCurPageJob + 1][3].ToString();
string strfn = Path.Combine(fullpath, proCode + Convert.ToString(DateTime.Now.ToFileTime()) + (idxCurPageJob + 1));
using (FileStream fs = new FileStream(@strfn, FileMode.Create, FileAccess.Write))
{
fs.Write(bgImg, 0, bgImg.Length);
fs.Flush();
fs.Close();
}
picBox2.Image = Image.FromFile(strfn);
picBox2.SizeMode = PictureBoxSizeMode.StretchImage;
picBox2.Refresh();
}
.....
}
catch (Exception ex)
{
StreamWriter sw;
DateTime dtLogFileCreated = DateTime.Now;
try
{
sw = new StreamWriter("Project Form crash-" + dtLogFileCreated.Day + dtLogFileCreated.Month + dtLogFileCreated.Year + "-" + dtLogFileCreated.Second + dtLogFileCreated.Minute + dtLogFileCreated.Hour + ".txt");
sw.WriteLine("### Server Crash ###");
sw.WriteLine("### Message : ###" + ex.Message + "### StackTrace : ###" + ex.StackTrace + "###Soruce : ###" + ex.Source + "### InnerException : ###" + ex.InnerException + "### Data : ###" + ex.Data + " ### END of LOG ###");
sw.Close();
}
finally
{
Application.Exit();
}
}
}
在 PC 中安装应用程序并重新启动 PC 并尝试打开应用程序时,应用程序会以这种形式崩溃。但是在重新启动应用程序(不是 PC)后,我没有遇到任何问题。
以下是错误日志详细信息。
### 服务器崩溃:### ### 消息:###找不到路径“C:\Program Files\Autoscan Pte Ltd\STK PTA\TEMP\1100101308609520058907460”的一部分。 ### StackTrace : ### 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(字符串路径,FileMode 模式,FileAccess 访问,Int32 权限,布尔用户权限,文件共享共享,Int32 缓冲区大小,FileOptions 选项,SECURITY_ATTRIBUTES secAttrs,字符串 msgPath,布尔 bFromProxy) 在 System.IO.FileStream..ctor(字符串路径,FileMode 模式,FileAccess 访问) 在 PTA.Forms.frmPIProject.SetCurrentPageProjects()###Soruce :###mscorlib### InnerException : ###### 数据:###System.Collections.ListDictionaryInternal ### 日志结束###
此错误发生在这行代码中
using (FileStream fs = new FileStream(@strfn, FileMode.Create, FileAccess.Write))
{
fs.Write(bgImg, 0, bgImg.Length);
fs.Flush();
fs.Close();
}
非常感谢您对此的帮助