C# 设置winform应用程序使用的文件夹的权限
我的winform应用程序定期从SQL数据库中提取一个flash文件,将其写入临时文件夹,在WebBrowser控件中显示,然后将其从临时文件夹中删除。如果临时文件夹不存在,我的应用程序将创建它。临时文件夹位于“System.Environment.CurrentDirectory”中 我的问题是临时文件夹的权限经常变为只读,然后我的应用程序无法删除该文件。有时问题会立即出现,有时我可以在问题出现之前运行应用程序数次 如何确保文件被删除 我添加了删除临时文件夹的代码,然后在每次写入时重新创建它,但这并没有解决我的问题 只有我的应用程序需要访问此文件夹,并且该文件夹仅保存这些flash图像 我曾想过使用通用的“temp”文件夹,但读了一些可能导致问题的文章 另外,当我将临时文件夹定位到“D:\”时,我也遇到了同样的问题 我正在Windows XP上使用VS2008。该应用程序将在XP、Vista和7上运行 这是代码C# 设置winform应用程序使用的文件夹的权限,c#,permissions,directory,C#,Permissions,Directory,我的winform应用程序定期从SQL数据库中提取一个flash文件,将其写入临时文件夹,在WebBrowser控件中显示,然后将其从临时文件夹中删除。如果临时文件夹不存在,我的应用程序将创建它。临时文件夹位于“System.Environment.CurrentDirectory”中 我的问题是临时文件夹的权限经常变为只读,然后我的应用程序无法删除该文件。有时问题会立即出现,有时我可以在问题出现之前运行应用程序数次 如何确保文件被删除 我添加了删除临时文件夹的代码,然后在每次写入时重新创建它,
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\\” 您可以使用
System.IO.Path.GetTempPath()
获取要使用的临时文件夹
我假设您在删除时遇到问题,因为文件被某个进程锁定。您可以在下次重新启动时使用
MoveFileEx
功能将其删除,从而绕过此问题。我认为访问问题来自另一个锁定文件的应用程序。执行这些操作的一个常见应用程序组是反病毒程序中的on-access扫描仪
要深入了解谁访问了您的文件,您应该深入了解谁将阻止您的文件
您还可以对代码进行一些更改:
//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);
}
似乎c#verbatim stringsOliver的语法高亮显示被破坏了,我是否应该把“fs.Close();”也放进去使用括号中“fs.Write(b,0,b.Length);”下方的行行?不,因为using语句为您创建了这一行。看看“System.IO.Path.GetTempPath()”会一直存在吗?它会永远存在吗?有没有一种方法可以阻止用户访问该目录?没有,我认为它根本不进行检查,相反,如果用户设置了TMP或TEMP环境变量(可能还有更多),它将使用这些目录,否则它将返回到Windows目录或我认为的其他目录。这样做很好,因为它让用户决定应该在哪里创建临时文件,因为这是他的机器,但您需要确认它存在,否则返回到其他目录(或者警告用户他的设置不正确)。