XML : My program won't let go of a folder after Move()

So I have to make this simple console program we need to use at work.

Basically we get XML files in on a specific location from a 3rd party recruitment portal we are using. Anyone from our HR department can go to this website, choose people they got for hiring and then export all information about the candidates as an XML file that we then need to extract information from.

I have a simple console going that checks a location for folders every five second and then it's going to extract information when it finds a folder with an XML file in it. This works fine. If it fails, it'll show an error and move the directory with the XML file in it to another spot so it won't keep trying.

Now lets say that the XML is malformed or something and it can be fixed by hand. That would be nice because then I can just cut the folder and put it back in the folder that the program is checking right?

Well turns out I can't, because when I try Windows tells me that the folder is open in another program already and thus I can't move it.

I have a feeling this code is responsible for locking the folder, but I am not sure:

  private static bool IsDirectoryEmpty(string path)  {      using (IEnumerator<string> en = Directory.EnumerateFileSystemEntries(path).GetEnumerator())      {          return !en.MoveNext();      }  }    

Otherwise here is my main loop:

  static void Main(string[] args)  {      String path = "omitted";      while (true)      {          if (!IsDirectoryEmpty(path))          {              String[] dirs = Directory.GetDirectories(path);              foreach (String dirPath in dirs)              {                  String[] files = Directory.GetFiles(dirPath);                  String xmlPath = "";                  foreach (String file in files)                  {                      if (file.Contains("xml"))                      {                          xmlPath = file;                      }                  }                    if (xmlPath.Length != 0)                  {                      try                      {                          XmlDocument doc = new XmlDocument();                          using (StreamReader oReader = new StreamReader(xmlPath, Encoding.GetEncoding("UTF-8")))                          {                              doc.Load(oReader);                          }                          Console.WriteLine("{0}: Adding user found in: {1}", DateTime.Now.ToLongDateString(), dirPath);                          bool result = AddUser(doc);                            String lettercode = doc.DocumentElement.FirstChild.ChildNodes[0].FirstChild.InnerText;                          String parentPath = "omitted";                            if (result == true)                          {                              Console.WriteLine("{0}: User found in: {1} was successfully added!", DateTime.Now.ToLongDateString(), dirPath);                              Directory.Move(dirPath, parentPath + "\\" + lettercode);                          }                          else                          {                              Console.WriteLine("{0}: User found in: {1} was not added!", DateTime.Now.ToLongDateString(), dirPath);                              String newPath = Directory.GetParent(dirPath).ToString();                              Directory.Move(dirPath, parentPath + "\\" + lettercode);                          }                      }                      catch (XmlException ex)                      {                          Console.WriteLine("{0}: XML Error: {1}", DateTime.Now.ToLongDateString(), ex.Message);                      }                  }              }          }          else          {              Thread.Sleep(5000);          }      }  }    

No comments:

Post a Comment