![]() |
| | #1 |
| Registered User Join Date: Oct 2006 Location: UK/Norway
Posts: 464
| Save files using c# 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();
}
|
| h3ro is offline | |
| | #2 |
| CSharpener 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 | |
| | #3 |
| Registered User Join Date: Oct 2006 Location: UK/Norway
Posts: 464
| Thanks, that solved my problem |
| h3ro is offline | |
| | #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 | |
| | #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 | |
| | #6 |
| Confused 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |