[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

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}.
0
dvplayltd
Asked:
dvplayltd
1 Solution
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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