0

I have a tracking program that saves a hidden XML file during a save or auto save in case the user accidentally closes the program and needs to load it back up. It is imported into a dataset and data table.

Here's the code for the WriteXML and ReadXML:

WriteXML:

 private void SaveloadFile()
    {
        string Timefor = Properties.Settings.Default.DateFormat;
        System.Data.DataTable dt = (System.Data.DataTable)dataGridView1.DataSource;
        string Filelocation = Properties.Settings.Default.SaveLocation.ToString() + "_" + System.DateTime.Today.ToString(Timefor) + ".xml";
        try
        {
            dt.WriteXml(Filelocation, XmlWriteMode.WriteSchema);
            FileInfo xm = new FileInfo(Filelocation);
            xm.Attributes = FileAttributes.Hidden;


        }
        catch(System.Exception err)
        {
            MessageBox.Show("Ah poop, Something went wrong\n" + err, "Oh poop", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

ReadXML:

        public void ReadXML(string Filepath)
    {
        try
        {
            dt.ReadXml(Filepath);
            ds.Tables.Add(dt);

            dataGridView1.DataSource = ds.Tables[0];

        }
        catch (System.Exception err)
        {
            MessageBox.Show("Ah poop, Something went wrong\n" + err, "Oh poop", MessageBoxButtons.OK, MessageBoxIcon.Information);
            CreateDataBase();
        }
    }

And it seems to work. Although after opening the application and trying to save current work, I get this.

This only happens when I load the program and try to save.

I've looked into the code and I think I boiled it down to the readxml not letting go after reading. Though I have some suspicions that the writexml isn't working right ether.

I've looked around and I can't seem to find a solution to this bug.

If anyone can tell me what's wrong with the code that'll be great!

The57thGamer
  • 13
  • 1
  • 3
  • Something is already accessing the xml file, so that is why it's throwing that error. – Ryan Wilson Mar 14 '18 at 18:25
  • @RyanWilson That's what I figured. So I'm assuming that my code is fine, some other application is using it. If that's the cause any tips on finding out what is accessing it? – The57thGamer Mar 14 '18 at 18:59
  • possible problem here: dt.WriteXml(Filelocation, XmlWriteMode.WriteSchema); FileInfo xm = new FileInfo(Filelocation); xm.Attributes = FileAttributes.Hidden; you are trying to use FileInfo on the same file right after calling dt.WriteXML which is using the same file. Just a guess... – Ryan Wilson Mar 14 '18 at 19:00
  • @RyanWilson Yes. I had that in there so the file can be set to hidden so the users won't accidentally delete it. I'll see if that changes anything if I remove that bit of code. – The57thGamer Mar 14 '18 at 20:16
  • Ok. Like I said that was just a guess from looking at your example code. – Ryan Wilson Mar 14 '18 at 20:19
  • @RyanWilson Well this is interesting. After deleting the previous xml file and let the program write new one after deleting the attributes bit of the code, it's not sprouting an error. Maybe something in the FileInfo was messing with it? Either way, I think I might just have the xml be saved in a different folder like the appdata folder or similar. Not sure if that'll actually stop the error, but it's a start. – The57thGamer Mar 14 '18 at 20:29
  • Well I'm glad you have made some head way. If there is anything else I can do to help, just shoot me a message. – Ryan Wilson Mar 15 '18 at 12:00

0 Answers0