Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Hi friends,
Good Morning.
Today i am back with one more problem.

how can we declare application level variable?

My client download a file.
the download file name format is such like this

abc_ABC_DDMMYYY1

as you can see after date there is a variable 1.
every time when my client download file.this variable need to get increment.

i want to add check on date.that when date get changed.this variable set to 1.
and again whenever my client download a file.this variable must increment its value.

how can i do this...

for Example
1st time file download with name...

abc_ABC_DDMMYYY1

2nd time
abc_ABC_DDMMYYY2

3rd time
abc_ABC_DDMMYYY3.


so how can i do this?
Posted
Updated 28-May-12 21:32pm
v2

1 solution

To declare an application wide variable, declare it in you app.config file

XML
<appSettings>
    <!-- Your variable goeas here as under -->
    <add key="your_variable_name" value="your_variable_value"/>
</appSettings>



To get your variable in code:

C#
string YourVariable= ConfigurationManager.AppSettings["your_variable_name"];


I hope this is what you were asking and that this answer helps :)

Edit: Added way to edit app.config file

C#
// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Remove("your_variable_name");
your_variable_new_value = your_variable_value + 1;
config.AppSettings.Settings.Add("your_variable_name", your_variable_new_value);
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");

Source: How to change app.config file at runtime[^]
 
Share this answer
 
v4
Comments
prince_rumeel 29-May-12 4:05am    
but how can i increment in my variable?
which is there after date in my file download format...

abc_ABC_DDMMYYY1
Umer Aziz Malik 29-May-12 4:13am    
added your questions' answer too :)
prince_rumeel 29-May-12 4:44am    
how can I increment in the value?
how can I assign value to this variable?

How can I use this value in my app?
How this variable get increment every time?
prince_rumeel 29-May-12 4:46am    
please read my main question again.plz
Umer Aziz Malik 29-May-12 6:12am    
The link i gave you discusses how to change app.config during runtime. And i certainly DID read ur original question. But the thing is, you need to do something on your own too. The example above shows how to delete an entry and then add an entry in the application settings. Now cant you even use that to do what you want to? If you cant then i guess no 1 here would be very happy to help you out!

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