设置winform应用程序使用的文件夹的权限

发布于 2024-08-31 17:53:19 字数 2484 浏览 1 评论 0原文

我的 winform 应用程序定期从 SQL 数据库中提取 flash 文件,将其写入临时文件夹,在 webbroswer 控件中显示它,然后从临时文件夹中删除它。如果临时文件夹不存在,我的应用程序将创建它。临时文件夹位于“System.Environment.CurrentDirectory”中。

我的问题是临时文件夹的权限经常变为只读,然后我的应用程序无法删除该文件。有时问题会立即发生,有时我可以运行应用程序几次才能发生。

如何确保文件被删除?

我添加了代码来删除临时文件夹,然后在每次写入临时文件夹时重新创建它,但这并没有解决我的问题。

只有我的应用程序需要访问这个文件夹,并且该文件夹只保存这些Flash图像。

我考虑过使用通用的“temp”文件夹,但在某处读到这可能会导致问题。

另外,当我将临时文件夹定位在“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
    }
}

任何建议或正确方向的观点将不胜感激。

干杯, Frederick

PS--将我的代码复制到这篇文章时,我看到 @"\images\" 之后的文本颜色全是红色;我的这部分代码是否有问题,或者这是一个显示工件?我应该用这个来代替:@"\images\\";

My winform application periodically pulls a flash file from an SQL database, writes it to a temporary folder, displays it in a webbroswer control, and then deletes it from the temporary folder. My application creates the temporary folder if it does not exist. The temporary folder is located in 'System.Environment.CurrentDirectory'.

My problem is the permissions of the temporary folder frequently become read only and then my application cannot delete the file. Sometimes the problem occurs immediately, and sometimes I can run the application several times before it occurs.

How do I insure that the file is deleted?

I added code to delete the temporary folder and then re-create it each time it writes to it, but this did not resolve my problem.

Only my application needs to access this folder, and the folder only holds these flash images.

I thought about using the generic 'temp' folder, but read somewhere that that could lead to problems.

Also, I got the same problem when I located the temporary folder at 'D:\'.

I'm using VS2008 on Windows XP. The application is to run on XP, Vista and 7.

Here is code.

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
    }
}

Any suggestions or a point in the right direction would be appreciated.

Cheers,
Frederick

PS--When copying my code to this post I see the color of the text is all red after the @"\images\"; Is there a problem with this part of my code, or is this a display artifact? Should I use this instead: @"\images\\";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷迭香的记忆 2024-09-07 17:53:19

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

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

You could use System.IO.Path.GetTempPath() to get a temp folder to use.

I assume that you're having problems with the delete due to the file being locked by some process. You might be able to get around that by using the MoveFileEx function to delete it at next reboot.

贱贱哒 2024-09-07 17:53:19

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

要更深入地了解谁访问了您的文件,您应该使用 进行更深入的了解进程监视器以找出谁将阻止您的文件。

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

//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);
}

I think the accessing problem comes from another application that locks the file. One common application group that does such things would be the on access scanner from your anti-virus program.

To get a deeper look into, who accesses your file you should take a deeper look with Process Monitor to find out who will block your file.

Also you can maybe make a little change to your code:

//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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文