C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-06-2009, 08:31 AM   #1
Registered User
 
Join Date: Oct 2006
Location: UK/Norway
Posts: 464
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,
h3ro is offline   Reply With Quote
Old 09-06-2009, 09:03 AM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
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
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 09-06-2009, 09:12 AM   #3
Registered User
 
Join Date: Oct 2006
Location: UK/Norway
Posts: 464
Thanks, that solved my problem
h3ro is offline   Reply With Quote
Old 09-06-2009, 09:31 AM   #4
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
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();
}
theoobe is offline   Reply With Quote
Old 09-06-2009, 10:06 AM   #5
Registered User
 
Join Date: Oct 2006
Location: UK/Norway
Posts: 464
I knew c# must have some nice xml functions. Thanks for sharing
h3ro is offline   Reply With Quote
Old 09-07-2009, 12:00 AM   #6
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
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.
Magos is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:07 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22