Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone!

I am developing a windows application,and i need to set some global variables,just like connection strings,paths etc.Then anytime the application runs,it should have this settings until changes are made to them.How can i achieve this? Maybe from a configuration file? Any help please ?

Thanks in advance ;)
Posted

Here[^] is a microsoft article about how to use settings. I think it may be closer to what you are looking for.
 
Share this answer
 
I found the solution with .ini file,there i store the settings.
thanks to this article An INI file handling class using C#[^]
 
Share this answer
 
Comments
Eugene Sadovoi 18-Apr-12 16:44pm    
This is outdated and no longer supported way of storing config info.
Better Way is to use the Module for that .
with the use of Module you can access all the public functions ,variables easily.then after your have to just create object of that and used it.
 
Share this answer
 
Comments
Eugene Sadovoi 18-Apr-12 16:49pm    
ConfigManager allows to combine configuration from different sources. It could be different files or different modules. See solution 4 for correct solution.
Hi,

You can use static variables that you can use as a global variable

Add a class in your project say "MyGlobalClass.cs" and write following code:

C#
namespace CodeProject
{
    public static class GlobalClass
    {
        private static string ConString;
                
        public static String MyConnectionString
        {
            get { return ConString;}
            set { ConString;= value; }
        }
    }
}


Here, you can use ConString variable as a global variable to store or retrieve your connection string

C#
GlobalClass.MyConnectionString= "Data Source=localhost\SQLExpress;User Id=sa; Password=pass; Initial Catalog=Deepak; "; // Set your connection string
string ConString = GlobalClass.MyConnectionString; // Get your connection string
 
Share this answer
 
Comments
IviKAZAZI 18-Apr-12 6:36am    
This is what i have done already,but i wanna know if,when i close the application and reopen it,the user input that i stored at the global variables are the same as last input,or they are refreshed? sorry for the bad english ;)
Deepak_Sharma_ 18-Apr-12 6:44am    
Then you have to store this information either in the database or in a text file.

One more thing why not you store these information in App.Config file. Store your connection string in <connectionstrings> and other settings in <appsettings>
IviKAZAZI 18-Apr-12 9:30am    
But the connection string must be an user input,it might change!
Eugene Sadovoi 18-Apr-12 16:43pm    
You should store this info in .config file. ConfigurationManager provides you with ways to use different storage facilities, one of which is config file.
See solution 4 for example.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900