Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6980405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:00:48+00:00 2026-05-27T18:00:48+00:00

I am trying to delete a file after i create it but simply cannot.

  • 0

I am trying to delete a file after i create it but simply cannot.
The error message is that it is still being used by the process.
I am working on a winform application.

here is my 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");

I have also tried the code below but the same error, File being used by another process

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

}

Am i missing a close of file anywhere?

Please help. Thanks.

here is the code for getconfigtag: it just creates a tags to be applied in the config file.

 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:

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:00:49+00:00Added an answer on May 27, 2026 at 6:00 pm

    “Am I missing a close of file anywhere?” You can be sure the file gets closed by using a ‘using’ statement.

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

    This code works for me, but so does a variation of your original, so beyond that, I’m not quite sure what the issue is.

    Addendum:

    It appears—by your stack trace—that you’re trying to email an XML file. If that’s the case, and you’re using SmtpClient, you don’t even need to write the XML document to a file.

    MemoryStream memoryStream = new MemoryStream();
    xmlDoc.Save(memoryStream);
    
    // ...
    
    mailMessage.Attachments.Add(
        new Attachment(memoryStream, "EmailConfig.xml", "application/xml"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to delete a Image file in WPF, but WPF locks the file.
I'm trying to write a windows batch file that can delete files from subdirectories.
I'm trying to delete a file, after writing something in it, with FileOutputStream .
I have a problem when I'm trying to delete an image file. I always
i am trying to delete some files using a batch file.. (winxp) my problem
I'm trying to delete a directory that contains XML files from a remote computer.
I'm trying to delete several working copy directories, but I get an Access Denied
I'm trying to rename a file after it's uploaded in the model's save method.
When trying to delete a directory (+ contents) and after reading the files inside,
I'm trying to do the following(in this order): Create a Rich Text Format file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.