Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 875
  • Last Modified:

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?
0
Tom Knowlton
Asked:
Tom Knowlton
3 Solutions
 
Tom KnowltonWeb developerAuthor Commented:
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.
0
 
softplusCommented:
Hey, you're using .NET -- use a XML config file :))
An example, to get some ideas:
http://www.thecodeproject.com/csharp/ConfigSectionHandler.asp
or in vb.net: http://www.freevbcode.com/ShowCode.asp?ID=5318
0
 
Bob LearnedCommented:
Tom, this is the article that I have as a resource:

Upgrade Your INI Files to XML with .NET:
http://www.devx.com/dotnet/Article/7008

Bob
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
cookreCommented:
>>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.
0
 
Tom KnowltonWeb developerAuthor Commented:
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?
0
 
Tom KnowltonWeb developerAuthor Commented:
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.
0
 
softplusCommented:
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.
0
 
Tom KnowltonWeb developerAuthor Commented:
>>>>>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.....
0
 
softplusCommented:
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) :))
0
 
Tom KnowltonWeb developerAuthor Commented:
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.
0
 
Tom KnowltonWeb developerAuthor Commented:
>>>>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!!!!!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now