Properties.Settings.Default.Save(); -> Where is that file - c#

I have app that uses Settings.
To save settings I use:
Properties.Settings.Default.Save();
To read tham I use:
Properties.Settings.Default.MyCustomSetting;
In my folder with application I have only exe file.
No config files. My application works good, can read write settings.
Where is that file located if it is not in application folder?

On My Windows XP machine, the settings are saved in a file called user.config somewhere under either C:\Documents and Settings\<UserName>\Application Data\ or C:\Documents and Settings\<UserName>\Local Settings\Application Data\
Update:
On Windows Vista and later, the locations have changed to C:\Users\<UserName>\AppData\Roaming\ and C:\Users\<UserName>\AppData\Local\

This depends on what SettingsProvider you are using. By default, this is the LocalFileSettingsProvider
Quoting from that page:
Application-scoped settings and the default user-scoped settings are stored in a file named application.exe.config, which is created in the same directory as the executable file. Application configuration settings are read-only. Specific user data is stored in a file named user.config, stored under the user's home directory.
They may also go to the %APPDATA%

On my Windows 7 machine, it's saved in:
Users\\AppData\Local\MyCompanyName\MyExeName\1.0.0.0\user.config
Replace MyCompanyName with something specific to you, and replace MyExeName with the name of your Exe. Mine is followed by a lot of random characters.
This threw me, too. I'm glad I'm not the only one. :) I hope this helps!

you can get the path programmatically:
var path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
Textbox1 = path;

I don't know the specifiek path but i think it's in documents and settings
Place a breakpoint on the Save and the path should be visable in one of the members/submembers of Properties.Settings.Default
see this post

Related

Where are settings saved wiithin the context of BricsCAD?

My C# DLL project has settings and the defaut values are held automatically in the xxx.dll.cong file.
When I perform a save of the settings:
Properties.Settings.Default.Save();
They are correctly read back in when I display my form. But I can't work out where the users saved version of the config file is kept? I tried to look in %localappdata% and nothing is there.
To summarise:
This is a .Net DLL tool bring run inside BricsCAD.
We are using the Properties.Settings concept.
Where is the users Save() version of the file kept?
Update
This is the closest conversation I could find about this subject (related to AutoCAD). It is still relevant because my tool is used by three CAD packages:
AutoCAD
BricsCAD
ZWCAD
But the linked discussion does not actually answer my question. Clearly the "settings" are persisting. But they are not persisting in the %localappdat% folder for my tool. I have now tested with both BricsCAD and ZWCAD and still can't find them.
According to this article the configuration file should be updated. But the config file where the DLL is is not updating. Even though the mechanism works. The persisted settings are somewhere else.
I decided to use the MS-DOS command findstr in a console window:
findstr /s "SaveToDXF_DuplicateCreateCount" *.config
I performned the test on my C drive and it came up with three files and one of them seemed to be the file of interest:
c:\Users\[USERNAME]\AppData\Local\Bricsys\DefaultDomain_Path_LOTSOFCHARACTERS\21.2.06.0\user.config
The LOTSOFCHARACTERS was a weird sequence of text. But when I open that user.config file it had my current settings.
I am not sure as the the reason for my settings being persisted in this manner.
This answer (https://stackoverflow.com/a/60117461/2287576) prrovided to a differeht question actually explains the aforementioned. To quote:
User Settings (Settings in the User scope) (applies to Visual Studio
2017+) These settings are stored along with the Application settings,
but have associated local storage files, used to store values that a
User (or the Application) can change at run-time:
2.1 A user.config file stored in the current User profile in the
[User]/AppData/Local/[CompanyName]/[ProductName].exe_<hash>/[Assembly Version]
section, if the setting's Roaming attribute is set to false.
2.2 A user.config file stored in the current User profile in the
[User]/AppData/Roaming/[CompanyName]/[ProductName]/[File Version]
section, if the setting Roaming attribute is set to true instead.

How to give a folder path in app.config file in C#

I am creating an application in WPF.
In that I am using XML file to store some settings.
My app will run for every 10 sec. So it will use that XML file settings.
My issue is in My local system i am calling the XML file as D://Foldername/projectname/test.xml .
But after deployment it is storing in C://Programfiles/Projectname/test.xml .
So how to give a generic path so that it runs in all the client systems.
I am creating setup file to install in clients systems.
Please help me.
Open the project properties page.
Click on Settings tab.
Add a new item called "MyPath". Make it an Application Setting of type String and give it a sensible default path name as value.
Reference the value in code with Properties.Settings.Default.MyPath.
If you open the applications config there will be a setting called MyPath where you can override the path at runtime.
I suggest you to put the XML file in the same folder as your EXE file and then use Assembly to get its current path.
var cfgPath = Assembly.GetExecutingAssembly().Location + ".config"
Update
it's better to name your config file the same with your exe file but with ".config" extension.
If you are really using ClickOnce, I hardly recommend you to create your own directory for data and configuration files:
private static string GetDataDir()
{
var dataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"YourApplicationName");
if (!Directory.Exists(dataDir))
Directory.CreateDirectory(dataDir);
return dataDir;
}
The problem with storing the data in the directory of the executable is, that it will be at a different location. While debugging, it will be in you \bin directory. When the application is deployed by ClickOnce, you gonna have a bad time. The installation directory for a ClickOnce application is created for every version. So if you EVER update your application at "customers", all their settings will be lost.

Where to save project related text files?

For testing and debugging purposes, I just hard-coded the file paths for the text files that would be used by my application, e.g.
const string CONFIG_FILE_PATH = #"C:\myconfigfile.txt";
But I don't think it's a good idea to leave it as it is in the Beta/Release version.
So I am wondering, what would be the best location for saving these configuration files that will be used / read by the application? Any suggestions?
Many Thanks.
Why not save the strings in the Settings section of your project? GRight click on your project in Solution Explorer, select Properties, go to the Settings section and add a new string with your file path. Then, in your code, you can access it like this:
using ProjectName.Properties;
var path = Settings.Default.MySetting;
To change the setting:
Settings.Default.MySetting = newPath;
Settings.Default.Save();
In the same folder as the executable.
But you should consider using a Settings class (you can read more here). Visual Studio can automatically create a strongly typed wrapper around a section in the app.config file.
The settings are stored in the same file as the executable, but can be overridden (and saved from the application) in a matching file in the user profile for each user.
Another option: If the test config file is intended to sit along side your executable, you can "Add" "Existing Item..." to your project and then change its properties to "Copy always" or "Copy if newer". During debugging, the executable should be able to find the copy of the config in its current working directory.
This isn't such a useful solution when there's a lot of testing to do.
For settings I would certainly use the app.config file. This is proposed by Microsoft and apart from that it is pretty much the easiest way to handle application settings anyway.
For other files I'd recommend either the applications local or roaming settings path, depending on weather you only need the data local or not. For compact, local databases I tend to use this approach. The application directory, as suggested by Albin, is a bad idea. You cannot be sure that the user is actually allowed to write to that directory and/or files in that directory (i.e. the app was pre-installed by an admin with elevated rights).
To get the location of the local paths use
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal)
and for the roaming path
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming)
More information on Windows User Profiles (the actual paths in different versions of Windows for example, you can find here: http://msdn.microsoft.com/en-us/library/aa372123.aspx

Location of applicationname.exe.config in C#

Where I can find the applicationname.exe.config file in my Windows form application.
Where can I find the application exe... mentioned that config file should be placed in Release or Debug files. But I cannot see the file in those places.
Also, I manually copied the config file from project root directory and past it in Release directory. Still the changes are not effected.
Then, I renamed the config as applicationname.exe.config, still the not effected.
Anybody can tell me how can I overcome ?
It should be in the same folder as applicationname.exe. That is generally where the config files are for win forms applications.
If it is not there, you can create a new one, with the name applicationname.exe.config.
Try like this path. i.e, inside bin folder
C:\Documents and Settings\deepika\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\bin\Debug
Try,
string path = Environment.GetCommandLineArgs()[0] + ".config";

Store user settings into application folder

I'm using setting from my C# application like this:
String str = Properties.Settings.Default.SETTINGS_NAME;
When I save this settings, a settings file is created on
C:\Documents and Settings\<user name>\Local Settings\Application Data\<comp name>\Keb.exe_Url_pbs4eg1o2ija22omldrwjxhdam0jxxi5\1.0.0.0\user.config
Is there a way to change this path to Application.ExecutablePath\user.config, and use it next time so my application can be more portable ?
You can control the location of the user.config file by creating a custom SettingsProvider. Luckily for you, someone at CodeProject already did that.
See my answer here for all the details: How to make designer generated .Net application settings portable
If you want it to be Single user or in other way make the configuration of your application portable i will use a custom config file like an .ini file and keep it my app's root folder.
That way any one want to have those settings can just copy it in his own app's root folder on some other computer. When app runs it just loads the settings and behave accordingly.
save data in a fixed format like
[setting_name] = [Setting_value]\n
or in XML file, with Tag name for setting and value for... well... value :)
You can also go with registry setttings but user don't feel it trivial to copy and merge .reg files
This is the way i have seen some PC Games (for eg. i frequently changed Crysis and MassEffect settings) and Softwares save their config files.

Categories

Resources