I am writing a program that will be running continually on a server. I would like to be able to change some of the inputs without hard coding requiring recompiling.
I know this is possible to do by using a text file, however I don't want to have a text file for each value.
I also know that I could use a table, but I also don't want to have a table with one row for this program.
Is there another solution?
As you can probably guess, I am new to the .NET/C# world, so pardon if this is too basic of a question.
Use the appsettings section of the app.config.
Depending on what you are trying to accomplish, Application Settings may also serve your needs. These are stored on the client computer. You can save user settings here, for example.
My preference is most cases is App.config (Web.config in ASP.NET), though, as has been suggested by other replies.
It sounds like you need one or more Config files, C# has built in support for an XML formatted Config File App.Config the access point to which lives in System.Configuration.ConfigurationManger.AppSettings
You have many possibilities:
Text file with separated values
XML file with element values
Web.config if your program is ASP.NET
App.config
Database (But not in your case)
Web Service from another program to (Maybe overkill)
Hard coded values in html (non compiled file)
The easiest is Web.config or App.config because .net have already tools for you when you want those values : ConfigurationSettings.AppSettings["Key"];
Not really clear what are you trying to acomplish but if i understood well you want a value in your program to change dynamically.
If thats the case you can use xml configuration the XmlReader and XmlWriter class in the System.Xml namespace.
Related
I know the easiest way to generate a config file is through visual studio. however the environment my program is going to be functioning in we are going to have several different configurations and the application needs to be able to build the config files on its own. Just curious if there is an easier way than making a large string literal and then copying over to a new file. Thanks for any help.
Not sure what kind of information you want to save in generated configurations.
If you are using only appSettings section which as only key values, then it would be better to generate a JSON file. It is very easy to generate it using newtonsoft.json.
in your app.config file you can keep the path of JSON file and load the settings at app startup if the file is already available.
NOTE:
JSON can also store any kind of complex configurations, you will have to generate the classes to hold those configurations.
Once you application puts value in these objects, serialize it to JSON and keep it in appropriate folder which is accessible to application.
Hope this helps.
in C# 3.5 (VS 2008) , how can i have a Resourse file or something like that, to have some of my config strings like webservice URLs that i can change them at runtime?
because of some reasons , i don't want it to be in app.config file.
here is the sample code that i like to have:
string s = Resource1.myURL;
Why can't? Just right click your project then Add Items->General->Resource File. Say you may have several resource files such as Resource.en-US.resx, Resource.zh-CN.resx and Resource.de-DE.resx. To manage these resources, you are supposed to have a class called ResourceManager which probably contains these methods:
public static GetResource(CultureInfo culture, string key)
{
//blabla
}
The ResourceManager is well initialized at the beginning of the application (for example, it's initialized in Global.asax if you are using asp.net webform), storing in the application cache.
Use a settings class
If you don't want to use app.config then simply make your own XML settings file. After all app.config is nothing more than that. You can customize it to the format that you want, and only inlcude the tags you need. Once it is finished you can than use LINQ to XML to parse through and change things as needed. If you are not familiar with linq than it might take a couple minutes of reading, but I think it would be well worth it.
I want to store settings for my C# application, such that default setttings can be easily shipped with my binaries and the end-user can change them using a simple text editor(or in some other simple way).
I seem to face several alternatives : a .config file, .settings file or a .resx file. What are the pros and cons of these?
Edit1: End-users are computer professionals mainly, so editing these files should not be much of a problem.
Edit2: The settings are something like connection strings, and some other parameters (mostly one-time stuff). Building some kind of GUI/API for changing them is not really an option. Also my application will not edit any of these values, so persistence through code is not required.
Yes, Project + Properties, Settings tab was designed to do this. Add your settings here, change the Scope to Application. That generates a app.exe.config file in your build direcctory, deploy it along with your EXE. Use Properties.Settings.Default.SettingName in your code to obtain the setting value. Your user will normally need admin privileges to edit the .exe.config file on the target machine to change the setting value.
The small print: settings do not work well for DLL assemblies, you have to merge the .config files by hand. When using the debugger, settings are retrieved from the app.vshost.exe.config file.
The .settings file is a helper file used by the IDE, ignore it. .Resx files store resources, they get compiled and embedded in a binary form in an assembly. They are not editable by the user.
I think you can have two ways of doing this.
For regular users, you can make a custom GUI that will make it simple for them to use.
For advanced users, they can edit the configurations using a text editor if it's stored in a text file (ini file, config file, etc..) or you can make an API.
The .settings file is typically used for user-specific preferences and configuration information (whereas the .config file is used for global settings for the application or anything that modifies the .Net runtime. Simply putting parameters in a .config file can alter the behavior of your application even without you writing a single line of code for it).
Check out the Settings article on MSDN for more: http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx
Since the file will be modified by the users, I think using app.config is not a good idea. What if they break the file structure? Or set an invalid value? Probably your application will crash directly.
One of the solutions would be to use a custom XML file. You will then validate it when your application starts. XSD will probably be the more elegant way to do it, but you can also parse it directly and validate it in code. If the file is invalid, instead of crashing, you will try to solve the problem, and if impossible, display a pretty error to the user, explaining that there is an error in XML at line n, position n, which is [error description here].
If the end user is really going to be editing them, I'm not sure I would want them editing my app.config file.
You have another couple alternatives that you haven't included. You could use an old-school .INI file that is simpler for an end user to understand. You could also use the registry. I would recommend the INI file, unless your users are very savvy, in which case use the .config file.
The answer depends on the deployment method. For instance, if you are using ClickOnce and offer updates, you might encouter problems using Application Settings.
I believe the best way to go is to create a GUI, something that is most certainly suitable for novice users. Given that you already excluded that option, use John's suggestion (ini files).
When you compile a C# project, the app settings from app.config are saved along with the exe file. For example, if the program name is "solve_np.exe", there will be a "solve_np.exe.config" next to it, making it look less neat than I want it to. How can you embed it into the .exe?
I tried to set Build Action to Embed Resource, but that did not do the trick.
Aha. I guess you're falling foul of Visual Studio automatically placing stuff in configuration that you DONT want configured by the end user.
In this case use resources. Simply add a new file of type .resx. With this you'll be able to add things like strings and images to the resource file. After you build this it should provide static access to the resources (it typically generates a code file from the resources for you so you don't have to access ResourceManager yourself).
E.G. If I made resources.resx with a string called MyConfigValue after a build I should be able to access this like:
textBox.Text = Resources.MyConfigValue;
If you want values that are kinda variable but shouldn't be configurable then this is the right place to put them.
HTH.
It isn't unprofessional to have an app.config file shipped alongside your exe. I think the word you may be looking for is untidy. I personally don't find this is the case myself however everyone is different! Perhaps you could simply make the app.config file hidden by default?
Or another alternative is to create your own config file which you could save to the App Data folder or even storing the settings in the registry instead.
Here's another factor to consider. Seasoned .Net developers are accustomed to the standard application design, which incorporates using config files in both web apps and desktop apps. If you depart from that practice, you will make it more difficult for any developers who follow you to understand what you have done. Even sa's on the web server may be confused by the absence of a config file.
Since pretty much everybody does it this way, your code does not look "untidy" to others. On the contrary, it would be befuddling not to see a config file where one is expected.
Typically the theory of a configuration file, is that it stores settings you may want to change once you've deployed the application (for something like user preferences). To do this, you need to be storing somewhere external to your application. If you use the default setup, you get "[Your].exe.config". There are many other options you could look at, but nearly every one of them ends up with a file written somewhere, if you providing a mechanism that saves settings of some kind.
I agree with serhio darasd and Quibblesome but you can just delete "solve_np.exe.config" and it'll just use default configs.
After considering what was written in these comments and other searching, I decided that the best way to handle my issue of wanting my Entity Framework connection string to be embedded into my executable instead of needing the MyApplication.exe.config file with my deployed solution was to created a derived class like such:
Public Class MyEFDataContainerLocal
Inherits MyEFDataContainer
Public Sub New()
MyBase.New(My.Settings.MyEFContainerConnectionString)
End Sub
End Class
I just created an Application Setting of type Connection String that contained the same string as what is found in the App.Config file. I just had to replace the "e;'s with actual quotes.
Then whenever I wanted to use the MyEFDataContainer as in Dim db As New MyEFDataContainer I would just use Dim db As New MyEFDataContainerLocal instead.
Hey, I want to store application configuration data. Currently, I'm using INIs but they are too old and now I want to try something new. I'm on .NET platform using C#.
What solutions do I have? app.configs?
The scenario here is like that, I want to let other programs read the configuration data.
Yes, the various .config files (app.config, web.config, etc..) have replaced INI and, to a certain extent, the Registry as the preferred means of storing configuration data for your application.
Any program, .Net or otherwise, can read those files. .Net has the ConfigurationManager class and other languages can simply do XML parsing to get the values they need.
The standard approach with .NET is to use app.config files for application settings and user.config files for user specific settings (the location varies between XP and Vista though).
There's nothing to stop other programs reading the configuration data, you just need to have a configuration setting in the second (or third) application that tells it where to look.
The app.config would be the preferred way of doing things in .NET. Should work fine to read from other applications as well, as long as you give them the right path to the file. And it's just an XML file, so you can read it from non .NET apps as well.
If you're using Visual Studio, you can use Application Settings for this purpose. Just open the automatically added Settings.settings or create another one. They will be automatically available in Properties.Settings.Default and are stored as XML. You can also have multiple settings files for different purposes.
This is a Visual Studio concept rather than a .NET concept.
I often use an SQL table to hold my application settings. I create a singleton class, ususally called AppSettings, that I load with data from the table. The AppSettings class is then used to get the config values instead of directly accessing the config files. For ASP.Net apps, I instantiate the AppSettings class in the Application_Start event in Global.asax.cs.
Doing this gives me a way to allow the user to control some of the app settings, e.g. an email address for notifications. It also can simplify things when maintaining prod, QA, and dev versions of the app (assuming you have separate database instances for each)