Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

INI file

What is an easy way to write-out and read-in basic Form initialization data?

For example...whenever the form closes....I want to record the form height and form width to some sort of INI file.

Then when the form opens it will read-in the INI file and set the form height and width to whatever it was last set to in the INI file.


Is there a "best practice" for doing this sort of thing?
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

The Windows Registry is an option....but a simple text file is probably better for now.  I don't want to rely on the Registry.
ASKER CERTIFIED SOLUTION
Avatar of softplus
softplus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmmm....


I am trying to implement a simple XML Serializer / Deserializer:





using System;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplicationXML
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  //
                  // TODO: Add code to start application here
                  //
                  Serialize();
                  Deserialize();
                  Console.ReadLine();
            }

            static public void Serialize()
            {
                  ClassXML cxml = new ClassXML();
                  XmlSerializer mySerializer = new XmlSerializer(typeof(ClassXML));
                  StreamWriter myWriter = new StreamWriter("tomtestxmltoday.xml");
                  myWriter.Flush();
                  myWriter.Close();
            }

            static public void Deserialize()
            {
                  ClassXML cxml = new ClassXML();
                  XmlSerializer mySerializer = new XmlSerializer(typeof(ClassXML));
                  FileStream myFileStream = new FileStream("tomtestxmltoday.xml",FileMode.Open);
                  cxml = (ClassXML)mySerializer.Deserialize(myFileStream);

                  Console.WriteLine(cxml.FirstName);
                  Console.WriteLine(cxml.LastName);

                  myFileStream.Close();
            }
      }
}





and the ClassXML:



using System;

namespace ConsoleApplicationXML
{
      /// <summary>
      /// Summary description for ClassXML.
      /// </summary>
      public class ClassXML
      {
            public string FirstName;
            public string LastName;

            public ClassXML()
            {
                  //
                  // TODO: Add constructor logic here
                  //
                  FirstName = "Tom";
                  LastName = "K";
            }
      }
}





What am I doing wrong?



An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll

Additional information: There is an error in XML document (0, 0).

The program '[2684] ConsoleApplicationXML.exe' has exited with code 0 (0x0).




Am I missing an attribute in the ClassXML??   [XmlAttrubute]  or something?
When I try and open the XML file in a browswer it says:


The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

XML document must have a top level element. Error processing resource 'file:///R:/IsElectDev/Electronics/Dev/Topaz/Software/Client/ISDrillInfo/ConsoleApplicationXML/bin/Debug/tomtestxmltoday.xml'.

 


I don't think my Serialize(  ) method call is working.
Avatar of softplus
softplus

I think your're missing something  :)
   mySerializer.Serialize(myWriter, cxml);
before you flush/close the file (flush isn't needed here)

+ you're really serializing an EMPTY class, i.e. you just created it before you're writing it; that doesn't make much sense, but I assume you're testing :)

If you want to take a look at the "raw" xml file, you can also open it in Notepad.
>>>>>I think your're missing something  :)
>>>>   mySerializer.Serialize(myWriter, cxml);
>>>>>before you flush/close the file (flush isn't needed here)


=========================


You mean the compiler can't read minds?



Yes, that was it....I missed that line while copying from an XML example on MSDN.....sheeesh.


It's working now.....
perfect :) - I thought that mind reading part was finally added in vs2003, but I guess we'll have to wait for vs2005 (or possibly later) :))
cookre:

>>I don't want to rely on the Registry.
Just about everything else does.

Besides, if you save info under HKCU, you have user specific saves.

=====================================================


This is a really good point.
>>>>perfect :) - I thought that mind reading part was finally added in vs2003, but I guess we'll have to wait for vs2005 (or possibly later) :))

LOL.....

Those Microsoft Slackers!!!!!