在创建文件后,我试图删除它,但根本无法删除。错误消息是进程仍在使用它。我正在做一个winform应用程序。
这是我的代码:
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.AppendChild(xmlDec);
XmlElement elmRoot = xmlDoc.CreateElement("testConfig");
xmlDoc.AppendChild(elmRoot);
GetConfigTags(xmlDoc, elmRoot, clientToken);
StreamWriter wText =
new StreamWriter(CommonCodeClass.configLocation + "EmailConfig.xml");
xmlDoc.Save(wText);
wText.Flush();
wText.Close();
wText.Dispose();
File.Delete(CommonCodeClass.configLocation + "EmailConfig.xml");我也尝试了下面的代码,但是相同的错误,文件被另一个进程使用
try
{
File.Delete(CommonCodeClass.configLocation + "EmailConfig.xml");
}
catch //or maybe in finally
{
GC.Collect(); //kill object that keep the file. I think dispose will do the trick as well.
Thread.Sleep(500); //Wait for object to be killed.
File.Delete(CommonCodeClass.configLocation + "EmailConfig.xml"); //File can be now deleted
log.Error(CommonCodeClass.configLocation + "EmailConfig.xml" + " was deleted forcefully as it was being used by the process.");
}我有遗漏任何地方的文件吗?
请帮帮忙。谢谢。
下面是getconfigtag的代码:它只创建要应用于配置文件中的标记。
internal static void GetConfigTags(XmlDocument xmlDoc, XmlElement elmRoot, string clientToken)
{
// Username Element
XmlElement elmUsername = xmlDoc.CreateElement(CommonCodeClass.xml_Username);
XmlAttribute xaUsername = xmlDoc.CreateAttribute("val");
xaUsername.Value = "singleVal";
elmUsername.InnerXml = "";
elmUsername.Attributes.Append(xaUsername);
elmRoot.AppendChild(elmUsername);
}StackTrace:
在C:\Users\ddsds\ 2008\Projects\ShareMgmt\Mgmt\CommonCodeClass.cs:line \Visual 2008\Projects\ShareMgmt\Mgmt\CommonCodeClass.cs:line 756 at ShareMgmt.UsersForm.btnConfigToAdmin_Click(Object发件人处的System.IO.__Error.WinIOError(Int32 errorCode,String maybeFullPath) at System.IO.File.Delete(字符串路径),在C:\User\ddsds\Documents\Visual 2008\Projects\ShareMgmt\Mgmt\UsersForm.cs:line 1122中
发布于 2011-12-28 15:50:35
“我在任何地方遗漏了一份文件吗?”您可以使用“”语句来确保文件被关闭。
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.AppendChild(xmlDec);
XmlElement elmRoot = xmlDoc.CreateElement("testConfig");
xmlDoc.AppendChild(elmRoot);
GetConfigTags(xmlDoc, elmRoot, clientToken);
using (StreamWriter wText = new StreamWriter(CommonCodeClass.configLocation + "EmailConfig.xml"))
{
xmlDoc.Save(wText);
wText.Flush();
}
File.Delete(CommonCodeClass.configLocation + "EmailConfig.xml");这段代码适用于我,但是你的原始代码的变体也是如此,因此,除此之外,我不太确定问题是什么。
增编:
从堆栈跟踪可以看出,您正在尝试发送XML文件。如果是这样,并且使用的是SmtpClient,那么甚至不需要将XML文档写入文件。
MemoryStream memoryStream = new MemoryStream();
xmlDoc.Save(memoryStream);
// ...
mailMessage.Attachments.Add(
new Attachment(memoryStream, "EmailConfig.xml", "application/xml"));发布于 2011-12-28 15:56:42
代码适用于我,但是每次使用实现使用的类的实例时,我都推荐使用IDisposable语句。
另一件事:永远不要打电话给GC.Collect(),强迫GC帮你清理。如果您正确地释放实例(使用using关键字,您不会忘记),那么GC不需要您告诉他该做什么。
发布于 2011-12-28 15:53:41
xmlDoc仍然有一个文件句柄。
将创建文件的代码放入函数中,以便xmlDoc在删除时超出作用域。您可能必须调用GC.Collect()。当然,这也是我遇到的最大问题之一。
或者,您可以将{}放在代码周围,而无需将其放入函数中。您可以始终使用{}来创建不同的作用域。
因此,您的上述代码变成:
// other preceeding code
{
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.AppendChild(xmlDec);
XmlElement elmRoot = xmlDoc.CreateElement("testConfig");
xmlDoc.AppendChild(elmRoot);
GetConfigTags(xmlDoc, elmRoot, clientToken);
StreamWriter wText =
new StreamWriter(CommonCodeClass.configLocation + "EmailConfig.xml");
xmlDoc.Save(wText);
wText.Flush();
wText.Close();
wText.Dispose();
}
File.Delete(CommonCodeClass.configLocation + "EmailConfig.xml");https://stackoverflow.com/questions/8657915
复制相似问题