none
What is the best way to remove hard coding in visual studio console application in C#?

    Question

  • What is the best way to remove hard coding in visual studio console application in C#?
    Thursday, June 30, 2016 5:10 PM

Answers

  • Refer DLLs for configuration manager:

    Create a app.config file.

    Add sample key values as below:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="countoffiles" value="7" />
        <add key="logfilelocation" value="abc.txt" />
      </appSettings>
    </configuration>

    Use it as below:

    using System.Configuration;
    string configvalue1 = ConfigurationManager.AppSettings["countoffiles"];
    string configvalue2 = ConfigurationManager.AppSettings["logfilelocation"];



    Regards,

    Farshid Mahdavipour, MCSE, PMP

    Blog: www.sharepointjunkies.com
    ---------------------------------------------------------------------
    Please don't forget to mark it as answered, if your problem resolved or helpful.

    Thursday, June 30, 2016 5:17 PM
  • One more thing that can be done is to refer an XML file in the code and parse them as below:

     public void ReadXmlFile()
        {
            string path = HttpContext.Current.Server.MapPath("~/App_Data"); // Finds the location of App_Data on server.
            XmlTextReader reader = new XmlTextReader(System.IO.Path.Combine(path, "XMLFile7.xml")); //Combines the location of App_Data and the file name
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        break;
                    case XmlNodeType.Text:
                        columnNames.Add(reader.Value);
                        break;
                    case XmlNodeType.EndElement:
                        break;
                }
            }
        }


    Regards,

    Farshid Mahdavipour, MCSE, PMP

    Blog: www.sharepointjunkies.com
    ---------------------------------------------------------------------
    Please don't forget to mark it as answered, if your problem resolved or helpful.

    Thursday, June 30, 2016 5:25 PM

All replies

  • Refer DLLs for configuration manager:

    Create a app.config file.

    Add sample key values as below:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="countoffiles" value="7" />
        <add key="logfilelocation" value="abc.txt" />
      </appSettings>
    </configuration>

    Use it as below:

    using System.Configuration;
    string configvalue1 = ConfigurationManager.AppSettings["countoffiles"];
    string configvalue2 = ConfigurationManager.AppSettings["logfilelocation"];



    Regards,

    Farshid Mahdavipour, MCSE, PMP

    Blog: www.sharepointjunkies.com
    ---------------------------------------------------------------------
    Please don't forget to mark it as answered, if your problem resolved or helpful.

    Thursday, June 30, 2016 5:17 PM
  • One more thing that can be done is to refer an XML file in the code and parse them as below:

     public void ReadXmlFile()
        {
            string path = HttpContext.Current.Server.MapPath("~/App_Data"); // Finds the location of App_Data on server.
            XmlTextReader reader = new XmlTextReader(System.IO.Path.Combine(path, "XMLFile7.xml")); //Combines the location of App_Data and the file name
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        break;
                    case XmlNodeType.Text:
                        columnNames.Add(reader.Value);
                        break;
                    case XmlNodeType.EndElement:
                        break;
                }
            }
        }


    Regards,

    Farshid Mahdavipour, MCSE, PMP

    Blog: www.sharepointjunkies.com
    ---------------------------------------------------------------------
    Please don't forget to mark it as answered, if your problem resolved or helpful.

    Thursday, June 30, 2016 5:25 PM
  • You can also use SQL server to store data in a table and refer them in the code.

    Regards,

    Farshid Mahdavipour, MCSE, PMP

    Blog: www.sharepointjunkies.com
    ---------------------------------------------------------------------
    Please don't forget to mark it as answered, if your problem resolved or helpful.

    Thursday, June 30, 2016 5:26 PM
  • Thank you, I liked the app.config approach.
    Thursday, June 30, 2016 5:27 PM
  • Consider the Settings tab of Project Properties too:

    It is possible to define not only strings, but also numbers and other types, which do not need special parsing.

    Then you can use expression like this:

      int some_constant = Properties.Settings.Default.SomeConstant;

    Thursday, June 30, 2016 6:06 PM
  • Viorel mentioned the easierst way. Visual Studio will offer full support inside the gui and will create code so that you do not need to work with Configuration Manager on your own. So I would suggest to check this possibility, too. (Just ention it, because you marked other more complex stuff as answer already!)

    Konrad

    Thursday, June 30, 2016 7:19 PM
    Moderator