gpt4 book ai didi

c# - 找不到路径的一部分 C#

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:27 24 4
gpt4 key购买 nike

我在 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)后,我没有遇到任何问题。

下面是错误日志的详细信息。

 ### Server Crash :###  ### Message  : ###Could not find a part of the path 'C:\Program Files\Autoscan Pte Ltd\STK PTA\TEMP\1100101308609520058907460'.  ### StackTrace : ###   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)     at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)     at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)   at PTA.Forms.frmPIProject.SetCurrentPageProjects()###Soruce : ###mscorlib### InnerException : ###### Data : ###System.Collections.ListDictionaryInternal ### END of LOG ###

This error is happening in this line of code

using (FileStream fs = new FileStream(@strfn, FileMode.Create, FileAccess.Write))
{
fs.Write(bgImg, 0, bgImg.Length);
fs.Flush();
fs.Close();
}

非常感谢您对此的帮助

最佳答案

也许你的目录没有被创建..最好不要吞下你的异常。把这段代码中的try/catch去掉,看看错误是不是在这里:

if (!exists)
Directory.CreateDirectory(folder);
else
{
Directory.Delete(fullpath, true);
Directory.CreateDirectory(folder);
}

我认为您的应用没有在 Program Files 文件夹中写入的正确权限。

关于c# - 找不到路径的一部分 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32452300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com