【发布时间】:2011-02-15 20:46:16
【问题描述】:
我的 winform 应用程序会定期从 SQL 数据库中提取 Flash 文件,将其写入临时文件夹,将其显示在网络浏览器控件中,然后将其从临时文件夹中删除。如果临时文件夹不存在,我的应用程序会创建它。临时文件夹位于“System.Environment.CurrentDirectory”中。
我的问题是临时文件夹的权限经常变为只读,然后我的应用程序无法删除该文件。有时问题立即发生,有时我可以在它发生之前运行应用程序多次。
如何确保文件被删除?
我添加了删除临时文件夹的代码,然后在每次写入时重新创建它,但这并没有解决我的问题。
只有我的应用程序需要访问这个文件夹,并且该文件夹只保存这些 Flash 图像。
我曾考虑使用通用的“临时”文件夹,但在某处可能会导致问题。
另外,当我将临时文件夹定位在 'D:\' 时,我也遇到了同样的问题。
我在 Windows XP 上使用 VS2008。该应用程序可在 XP、Vista 和 7 上运行。
这是代码。
DataSet dsFlashQuizRandom = new DataSet();
dsFlashQuizRandom = objUserDAO.GetFlashQuizRandom(intAge);
if (dsFlashQuizRandom.Tables[0].Rows[0]["large_image_blob"] != null && dsFlashQuizRandom.Tables[0].Rows[0]["file_name"].ToString().Trim() != string.Empty)
{
byte[] b = (byte[])dsFlashQuizRandom.Tables[0].Rows[0]["large_image_blob"];
if (b != null)
{
string flashFileName = dsFlashQuizRandom.Tables[0].Rows[0]["file_name"].ToString().Trim();
string targetPath = System.Environment.CurrentDirectory.ToString() + @"\images\";
string strFileName = targetPath + flashFileName;
//Delete the current version of the folder (if it exists); then create a new version of it.
if (System.IO.Directory.Exists(targetPath))
System.IO.Directory.Delete(targetPath, true);
if (!System.IO.Directory.Exists(targetPath))
System.IO.Directory.CreateDirectory(targetPath);
//Write the file to a FileStream, assign that stream to the webbrowser control.
FileStream fs = new FileStream(strFileName, FileMode.CreateNew, FileAccess.Write);
fs.Write(b, 0, b.Length);
fs.Close();
webBrowserQuizFlash.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowserQuizFlash_DocumentCompleted);
webBrowserQuizFlash.Url = new System.Uri(strFileName, System.UriKind.Absolute);
}
}
//Delete the Flash Webbrowser file once it has completed loading.
private void webBrowserQuizFlash_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
FileInfo fi = new FileInfo(strFileName);
try
{
fi.Delete();
}
catch (IOException ex)
{
MessageBox.Show("IOException = " + ex); //test code
}
}
任何建议或正确方向的观点将不胜感激。
干杯, 弗雷德里克
PS--将我的代码复制到这篇文章时,我看到 @"\images\" 之后的文本颜色全是红色;我的这部分代码有问题,还是显示工件?我应该改用这个吗:@"\images\\";
【问题讨论】:
-
似乎我们的语法高亮在 c# 逐字字符串中被破坏了
标签: c# permissions directory