【问题标题】:Set permissions on folder used by winform application设置 winform 应用程序使用的文件夹的权限
【发布时间】: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


【解决方案1】:

您可以使用System.IO.Path.GetTempPath() 获取要使用的临时文件夹。

我认为由于文件被某个进程锁定,您在删除时遇到问题。您也许可以通过使用 MoveFileEx 函数在下次重新启动时将其删除来解决此问题。

【讨论】:

  • 您好,'System.IO.Path.GetTempPath()' 会一直存在吗?它会一直存在吗?用户有没有办法阻止对该目录的访问?
  • 不,我认为它根本不检查,而是如果用户设置了 TMP 或 TEMP 环境变量(可能还有更多),它将使用这些目录,否则它会回退到 Windows 目录或我认为的东西。所以这很好,因为它让用户决定应该在哪里创建临时文件,因为它是他的机器,但你需要确认它存在,否则会退回到其他目录(或警告用户他的设置是不正确)。
【解决方案2】:

我认为访问问题来自另一个锁定文件的应用程序。执行此类操作的一个常见应用程序组是您的防病毒程序中的访问扫描程序。

要更深入地了解谁访问了您的文件,您应该使用Process Monitor 更深入地了解谁会阻止您的文件。

您也可以对您的代码进行一些更改:

//Write the file to a FileStream, assign that stream to the webbrowser control.
using(FileStream fs = new FileStream(strFileName, FileMode.CreateNew, FileAccess.Write))
{
    fs.Write(b, 0, b.Length);
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 2011-02-01
  • 2014-09-22
  • 1970-01-01
相关资源
最近更新 更多