initiating a string from a config file - c#

I am asked to do this and I have no clue from where to start since I am new to it.
I have created a C# .NET application (a web api to be more precise) using Visual Studio 2012.
I created an lsi file ( the new version of msi files) to be able to deploy it on a server.
Now I want to create some sort of a configuration file where I can edit a string without having to go into the code every time to change it every time.
lets say the variable is called mystring:
1- how can I create a config file that request a string without having to go into the code?
2- how can I say the mystring=input string ?
I tried to look it up but since I don t have the exact name I don t really know what I am searching for ...
Thanks,

One solution would be to save a configuration file with the settings you want to load, I'd suggest saving it to the ProgramData folder.
I would save the settings to an XML file (or JSON) so you can serialize and deserialize the data into your program when it loads. Guide
You can also save other details so they'll persist each time the application is opened this way, such as a username field.

Related

Not overwriting saved xml files in C# Window Form

I am fairly new to C# and I was wondering how to keep an XML file from being overwritten if one already exists on the install. In the application, there are two files that contain info to connect to the Database. One of them is relatively dynamic, but the other is saved at the setup. If I do a publish and try to update the application it always overwrites both files. Any thoughts?
You can check if the file exists with File.Exists(Path)
You can look here for more information:
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.exists?view=netcore-3.1
If this does not work you can try to read that file and if there is any data there just sont delete it.
I assume you are refering to configuration settings.
While designing your settings, in the designer, set scope to "User". This will bind the setting to the users local app settings, and will not be overwritten.
More info here: https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-create-a-new-setting-at-design-time

C# Change File Location For Next Time Program Runs

I am relatively new to C#, however I do have some basic knowledge of code from courses in high school and university. However, there is one thing I have not been able to figure out over the years. I am currently making a Form Application for a database system that stores information in a List using Visual Studios 2010.
On my main form; when the save button is pressed, the information is then serialized into an XML file. When the information is loaded, the information is then deserialized and put into the List for use in the code. All this is working correctly.
This process of saving and loading is done based on a string which contains the file path. This string is the location of a folder on my desktop (I put it there for easy access), and I am able to change the string in the code to basically move where the information is stored.
However, I have a separate "Admin" form which is able to change this file path string. When the user clicks the button to change the file path, I get the input from a text box, check its formatting, move the current file to the new location and update the location for the save method so changes can be saved before the program is closed. From there, the program reacts the same way as if I had changed the string from inside the code.
The problem occurs when I close the program. I do not know how to tell the program when it runs again that the location has been changed from the default and look for the file in the new location. The program reacts just like the file was missing (like it should) when it looks in the default location.
So basically, how do I tell the program that the save location was changed from when it was last run so it knows to load the info from a new location?
I have tried looking for an answer since high school (about 2 years ago) and have not found a solution. As a result I usually just keep the save location as the default (which I set it to) and don't try to change it. But this time, its important that the save location can be customized. My experience with Visual Studios is limited, as everything I know is from messing around with the program and looking up stuff when needed.
If needed, I can post snippets of my code. Thank you in advance!
It seems like what you really want is to save some user-defined settings for recall at run-time. Here is a MSDN link describing some basic conventions for storing / retrieving these settings.
https://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx
A *.config file would suffice (depending on the scale of the application).
Otherwise, you may want to go down the route of storing these settings in a database (if the scale is rather large, or if user-authentication is required for the application).
Here is another previous question dealing with this same subject (regarding App.config files):
What is App.config in C#.NET? How to use it?
I recommend using a config file where the .exe is, and write the location there, then read it in on program startup.
In particular .net provides this class which can manage your config file for you (assuming you have an app.config in your solution, otherwise create one)
https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx

Read binary file from wix setup at installation step

I`m having some trouble trying to modifying an sql script, marked in wix as Binary file.
Basically, what I want to do is read the file (binary file as it is declared), replace some values in it based on properties set in wix, and save the file, then run it as an sql script.
What I have tried so far: Getting the target directory with session.GetTargetPath("TARGETDIR"), but it returns me with wrong path, not the one where the script is initially extracted(such as a temporary folder) so i can modify it, then save it and make sure it will run the modified script.
My question is:
HOW can I get the path of a binary file at installation step, or how can I access it via session or w/e?
To mention: I have tried declaring it as a file, and later I can not run it as an sql script, cause it requires a binary, not a file.
Thanks!
You can use the formatted string format for entries in the file-table, i.e. [#filekey] to get the full path of the file. You should then be able to either set a property / CustomActionData-property and read it within a VBSScript.
Please note also the following (extract from the linked page):
The value of [#filekey] remains blank and is not replaced by a path until the installer runs the CostInitialize action, FileCost action, and CostFinalize action.

Saving user configuration options

I am creating a winform application in .net 2 with c#. I need to be able to save user configuration data and I am considering using an XML file for this propose. What is the general feeling for saving user configuration data? I have read that it is not in vogue to write to the registry but rather to a file instead. Please write your thoughts.
One option is to add it to settings file. In Visual Studio, go to My Project -> Settings and add it there. You can access it this way: C# - properties.settings.default...; VB - My.Settings...
Adding a setting to settings file stores the value in app config file and auto generates a class property for easy access to the value.
Use the ConfigurationManager class to store the settings.

How can I store user-tweakable configuration in app.config?

I know it is a good idea to store configuration data in app.config (e.g. database connection strings) instead of hardcoing it, even if I am writing an application just for myself. But is there a way to update the configuration data stored in app.config from the program that is using it?
If you use the Settings for the project, you can mark each setting as either application or user.
If they're set as user, they will be stored per-user and when you call the Save method it will be updated in the config for that user.
Code project has a really detailed article on saving all types of settings.
app.config isn't what you want to use for user-tweakable data, as it'll be stored somewhere in Program Files (which the user shouldn't have write permissions to). Instead, settings marked with a UserScopedSettingAttribute will end up in a user-scoped .config file somewhere in %LocalAppData%.
I found the best way to learn this stuff was to mess with the Visual Studio "Settings" tab (on your project's property pages), then look at the code that it generates and look in %LocalAppData% to see the file that it generates.

Categories