Thread: Save files using c#

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Save files using c#

    Hey,

    I am having a problem with saving a file using c#. I am using the following code to save a xml file. I want the file to be saved right next to where the .exe file is. The following works most of the time, but if I use a SaveFileDialog anywhere else in the program, the path that I set there is used when saved the settings file as well and I cant understand why. Any ideas on why it is happending?

    Code:
    private void saveSettings()
    {
        string output = "<?xml version=\"1.0\" encoding='UTF-8'?>\n";
        output += "<Settings>\n";
        output += "\t<fps>" + m_fps.ToString() + "</fps>\n";
    
        output += "\t<pivSize>" + m_pivotSize.ToString() + "</pivSize>\n";
        output += "\t<rectLineWidth>" + m_rectLineWidth.ToString() + "</rectLineWidth>\n";
        output += "\t<vertexLineWidth>" + m_vertexLineWidth.ToString() + "</vertexLineWidth>\n";
        output += "\t<vertexSize>" + m_vertexSize.ToString() + "</vertexSize>\n";
    
        output += "\t<makeHeader>" + m_makeHeaderFile.ToString() + "</makeHeader>\n";
        output += "\t<headerFilePath>" + m_headerFilePath + "</headerFilePath>\n";
    
        output += "</Settings>";
    
        // Write the whole thing
        TextWriter tw = new StreamWriter("Settings.xml");
        tw.WriteLine(output);
        tw.Close();
    }
    Regards,

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    SaveFileDialog changes current directory to the new one, look at the RestoreDirectory property is you want to disable this behavior...

    Or take the Application.StartupPath property at the startup of the Application and use the fully qualified path when you create your StreamWriter
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thanks, that solved my problem

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    I use AppDomain.CurrentDomain.BaseDirectory to achieve the path to the exe file. Also there are some very nice XML helpers built into .net which you could use to build your settings file.

    Code:
    using System.Xml;
    Code:
    private void saveSettings()
    {
        XmlWriterSettings appearance = new XmlWriterSettings();
        appearance.Indent = true;
        XmlWriter xml = XmlWriter.Create(AppDomain.CurrentDomain.BaseDirectory + "Settings.xml", appearance);
    
        xml.WriteStartDocument();
        xml.WriteStartElement("Settings");
    
        xml.WriteElementString("fps", m_fps.ToString());
        xml.WriteElementString("pivSize", m_pivotSize.ToString());
        xml.WriteElementString("rectLineWidth", m_rectLineWidth.ToString());
        xml.WriteElementString("vertexLineWidth", m_vertexLineWidth.ToString());
        xml.WriteElementString("vertexSize", m_vertexSize.ToString());
        xml.WriteElementString("makeHeader", m_makeHeaderFile.ToString());
        xml.WriteElementString("headerFilePath", m_headerFilePath);
    
        xml.WriteEndElement();
        xml.WriteEndDocument();
        xml.Flush();
        xml.Close();
    }

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I knew c# must have some nice xml functions. Thanks for sharing

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Or you could use:
    Code:
    System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. fopen vs. _open (for BIG image files)
    By reversaflex in forum C Programming
    Replies: 3
    Last Post: 04-01-2007, 12:52 AM
  3. Save files move when folder moves
    By mill1k in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2004, 04:35 PM
  4. can't save 13th record in files
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 07-15-2002, 10:08 PM
  5. How do you make save files?
    By Esparno in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2002, 06:05 PM