c# - 创建后删除文件

标签 c# asp.net winforms file

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

相关文章:

c# - winform c#中的弹出窗口

c# - 如何在 session 变量中保存多个值

c# - Elasticsearch/Nest-使用MultiMatch TextQueryType.Phrase和通配符

Javascript 和 ASP .NET 问题

winforms - 使用 VS 2015 Professional 创建无需管理员权限即可运行的设置

c# - 隐藏一个组框并删除它在 winform 中存在的空间

c# - Entity Framework 迁移在使用流畅 API 的扩展方法时抛出异常

c# - 如何检测是否按下了任何键

asp.net - 带有 AJAX 动态创建行的 jQuery UI Datepicker

asp.net - 检查 IE 浏览器是否为 .NET