What object to use to save custom data for WinForm app?

 Hi experts!

I'm new to C# 2008 NETFramework 2.0 with WinForm application. I need to save grid columns width and I wander with what object to do this? In COM world I do this in Windows Registry, fast and simple, in NET should I use exe.config  file ? Or other object ?

Look on code bellow:
        public static void SaveGrdWight(Form frm, FpSpread grd)
        {

            for (int i = 0; i < grd.Sheets[0].ColumnCount; i++)
                  {

                int Wight = grd.Sheets[0].Columns[i].Width;
                    // I need to save Wight in property with name:
                    // frm.Name + grd.Name + i.ToString();
                // this property may exist, but may not exist so it need to be created if not exist
                   
                  }

        }

Can somebody help me and to complate function with object with to be used to save this wildts? I need and example how to read it in loop. You can replace  grd.Sheets[0]. in code with other, for example int[] m={1,2,3,4,5}.
dvplayltdAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CoyotesITCommented:
You could save this in the registry or a .config my preference is normally a .config for saving settings for the application.

If you are saving the options on a per user basis on the machine either registry or file system, just use the HKCU, or the users %AppData% env var.

Something such as an xml output like


   
       
           
           
           
           
           
       
   


Here is a class I built you can modify to fit your application needs.

Good luck

~CoyotesIT



using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
 
namespace csCodeGen
{
    class CApplicationSettings
    {
        public void SaveSettings(string sSQLServer, string sNamespace, string sSaveToPath, string sUserName, bool bTrustedAuthentication)
        {
            SQLServer = sSQLServer.ToUpper();
            Namespace = sNamespace;
            SaveToPath = sSaveToPath;
            UserName = sUserName;
            TrustedAuthentication = bTrustedAuthentication;
 
            try
            {
                Save();
            }
            catch
            {
            }
        }
 
        public void GetSettings()
        {
            try
            {
                XmlTextReader xtr = new XmlTextReader(XmlDoc);
                xtr.WhitespaceHandling = WhitespaceHandling.None;
 
                while (xtr.Read())
                {
                    if (xtr.IsStartElement())
                    {
                        if (xtr.Name == "sqlserver") SQLServer = xtr.ReadInnerXml();
                        if (xtr.Name == "namespace") Namespace = xtr.ReadInnerXml();
                        if (xtr.Name == "output") SaveToPath = xtr.ReadInnerXml();
                        if (xtr.Name == "trustedauthentication") TrustedAuthentication = bool.Parse(xtr.ReadInnerXml());
                        if (xtr.Name == "username") UserName = xtr.ReadInnerXml();
                    }
                }
 
                xtr.Close();
            }
            catch
            {
            }
        }
 
        private void Save()
        {
            XmlTextWriter xtw = new XmlTextWriter(XmlDoc, null);
            xtw.Formatting = Formatting.Indented;
            xtw.WriteStartElement("config");
            xtw.WriteElementString("sqlserver", SQLServer);
            xtw.WriteElementString("namespace", Namespace);
            xtw.WriteElementString("output", SaveToPath);
            xtw.WriteElementString("trustedauthentication", TrustedAuthentication.ToString());
            xtw.WriteElementString("username", UserName);
            xtw.WriteElementString("lastrun", DateTime.Now.ToString());
            xtw.WriteEndElement();
            xtw.Close();            
        }
 
        public string SQLServer
        {
            get
            {
                return m_sSQLServer;
            }
            set
            {
                m_sSQLServer = value;
            }
        }
 
        public string Namespace
        {
            get
            {
                return m_sNamespace;
            }
            set
            {
                m_sNamespace = value;
            }
        }
 
        public string SaveToPath
        {
            get
            {
                return m_sSaveToPath;
            }
            set
            {
                m_sSaveToPath = value;
            }
        }
 
        public string UserName
        {
            get
            {
                return m_sUserName;
            }
            set
            {
                m_sUserName = value;
            }
        }
 
        public bool TrustedAuthentication
        {
            get
            {
                return m_bTrustedAuthentication;
            }
            set
            {
                m_bTrustedAuthentication = value;
            }
        }
 
        private string XmlDoc
        {
            get
            {
                return m_sXmlDoc;
            }
            set
            {
                m_sXmlDoc = value;
            }
        }
 
        private string m_sSQLServer = "";
        private string m_sNamespace = "";
        private string m_sSaveToPath = "";
        private string m_sUserName = "";
        private bool m_bTrustedAuthentication = true;
        private string m_sXmlDoc = Application.ProductName.ToString() + ".config";
        
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.