gpt4 book ai didi

c# - 创建后删除文件

转载 作者:行者123 更新时间:2023-11-30 14:01:16 25 4
gpt4 key购买 nike

我想在创建文件后删除它,但根本做不到。错误消息是它仍在被进程使用。我正在开发一个 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);
}

堆栈跟踪:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) at ShareMgmt.CommonCodeClass.EmailTheConfigFile(String userEmail, String clientToken) in C:\Users\ddsds\Documents\Visual Studio 2008\Projects\ShareMgmt\Mgmt\CommonCodeClass.cs:line 756 at ShareMgmt.UsersForm.btnConfigToAdmin_Click(Object sender, EventArgs e) in C:\Users\ddsds\Documents\Visual Studio 2008\Projects\ShareMgmt\Mgmt\UsersForm.cs:line 1122

最佳答案

“我是不是遗漏了一个文件的结尾?”您可以确定使用“using”语句关闭文件。

    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"));

关于c# - 创建后删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8657915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com