This is a weird question because i tought about it for some time and it doesnt really make sence to me because the program would need to re-compile, but, because if it is possible it would make my program much simpler, i need to know it there is a way to do it.
Basicly i have a .exe program.
The program has a combobox, that gets its value off a .txt file.
Its pretty simple to edit the values on the combobox, all the user has to do is to edit the .txt file and restart the program, but I need to make so that, if we move the .exe to another computer, it brings the .txt with him, so i added the .txt to my project.
The problem:
I need the user to, if needed, edit my .txt file.
If he edits the .txt file, it means i would have to update the file embedded in my .exe and from what i have searched, its not possible to do that.
My idea to solve this:
When the program starts, i copy the .txt out of the program, delete the embedded file on the project, and when the user closes my program, it copies the .txt that's on the outside, inside, like if i were to manually add it to the program again, that way if the user did edit it, if he moved the .exe to another computer, it would have its combobox values updated. It makes some sence to me, thats why im curious to see if it is possible, and if so, how do i add a external file to my project while it is running.
Thanks!
You could try the other way around.
You can add an application configuration file and in that you can maintain the values for your comboBox inside appSettings section with comma separated values. During the load it will take from config and during closure of the application you just need to update the appSettings section with the updated value.
Code to update the configuration file:
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings["comboKey"].Value = "comboValues with comma separated";
config.Save(ConfigurationSaveMode.Minimal);
When you are copying your application to some other system then you should copy the app.exe.config along with the app.exe file.
If you are trying with any installer file in different system and you would to have a user settings, then you could also use the Settings.setting file with User level scope variables.
Related
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
I have a C# app which reads from and writes to an Access database. There is one database file per user. My intention is to check for the existence of the MDB in the user's My Documents folder at launch, and if the MDB isn't found, then copy the template MDB to that folder.
I have already added the template MDB to my project and placed it in its own folder which I have called Packaged. However, I am unable to refer to this Packaged folder from code as it doesn't appear in IntelliSense.
My intention had been to use File.Copy to copy the MDB over, but I cannot determine the file path as I can't access the MDB in code. And presumably it wouldn't have a file path anyway if it's just packaged in the .exe?
What would be the best way to achieve this, given that I would rather not distribute a separate MDB if possible?
I've done this in the past by making the MDB an embedded resource, and writing it to disk as needed (if it doesn't exist).
Here is more info on writing an embedded resource to file.
You'd almost have to use one of the Environmental paths for the template MDB. Like ApplicationData or Documents and Settings\Username\Local Settings\ or one of the other ones. Local User Data is the best way for user specific data. IN the code in the beginning, determine to see if the file exists in the first run. If it exists copy the template, if not don't.
That way the user has full read and write access and the ability to copy the file or duplicate the file without security problems. These environmental variables are accessible through
Environment.GetEnvironmentVariable
You could also have a registry setting and read and write to the registry for that specific application, that has a simple DatabaseAvailable key, and toggle it yes or no.
You could also embed the MDB as a resource too, and then write it as necessary.
I have a WCF service, it response with JSON. I need to create a language file, which I can edit on production server. no problem if I will need to recycle App pool.
I was about to use Resource file, but I was worry that it is not editable by end user.
I don't need to edit it pragmatically, the end user will edit it by opening the file in notepad without recompiling the application.
What do you suggest?
Yes you can using the ResXResourceWriter class.
If you need to generate the Designer.cs file as well see this question Programmatically generate Designer.cs for resx file (ResXResourceWriter/ResXResourceReader)
If you need to modify the existing resx files see this question Modifying .resx file in c#
According to MSDN, you can add new resource at runtime:
You can incrementally add resources for new cultures after you have
deployed an application. Because subsequent development of
culture-specific resources can require a significant amount of time,
this allows you to release your main application first, and deliver
culture-specific resources at a later date.
http://msdn.microsoft.com/en-us/library/sb6a8618%28v=vs.110%29.aspx
I think editing current resource will also work.
Your users should be able to edit the files with no problems, the resource files are XML files that can be opened in notepad or any text editor, they could even open it in Excel and get multiple columns that they can easily edit.
This will require recycling your App pool but you're open to that.
[edit]
You don't need to be recompile as I mentioned before if your resource files are marked as content, but your App pool will be recycled to pick up changes
I am not sure but it seems that user can't edit resource file using a notepad at runtime, and the application should rebuild in order changes takes effect.
Setting file with user scope can do the job.
I have an app that has search functionality. The search algorithm is compiled to a separate dll. In the C# code for the search algorithm, I am using strings held in a settings file to point to the directory where the search index resides. But once the search code is compiled, the settings file is somehow incorporated in the dll. I want to have multiple versions of this code running on my server with each pointing to a different location for the index. And I want the operator to change a file to have each version point to something else as they find necessary. Both config files and settings files end up getting incorporated in the dll. How do I then accomplish this? What is the right industry standard way of doing this?
It's strange that the settings file is compiled... are you sure about that? Setting, config and resx files should be copied to the output directory, it's even a property you can modify on solution explorer. Then you should get it's values by doing
System.Configuration.ConfigurationManager.AppSettings.Get("YourKey")
But I think this won't know about user changes until app is restarted. If you want settings to be dynamic you should either store them on a database, or on a file that you open, read and close every time you need it.
Hope this helped!
So for example, when I load word, if I go to save a file, it will default to the same directory that I selected last time...Also, it keeps track of the last 10 (or whatever) .doc files you opened
how does it do this? Right now for a program I am writing (in C#), I just save a text document which holds these kinds of settings...is this bad practice??
If not, where should I put this text doc. Right now I am just using:
Path.GetDirectoryName(Application.ExecutablePath);
as the directory where this file is held...its fine before I publish the program, because it just uses one of the folders in the solution directory...
But after I publish it, the directory is really weird:
C:\users\me\AppData\Local\Apps\2.0\J6AAL16C.2QW.....
and it goes on....So is this like a directory created for this program when I install it?? is this where it SHOULD be getting saved?
Thanks!!
The Application Settings feature of .NET makes this pretty simple, really. In particular I wouldn't use the registry if I were you - it makes it harder for users to copy settings from machine to machine, etc.
It does get a bit weird if you try writing your own settings providers though - I've tried to understand the overall design a couple of times, and always got lost. For simple applications though, it's easy.