Create INI file for use in C#.NET - c#

just wanted some help in creating a INI file in windows using notepad to declare paths used for files/directories for a C#.NET application in Visual Studio.
Currently the program uses hard-coded paths in the application to location of files (on the network drives) used for input and output. What I want to do is use this INI file to declare these locations for files on my local machine so I can test out the program and then read it by the C#.NET program without modifying the production source code.
How do I go about doing this and how is it used once file is created?
Thanks

maybe you could use App.config:
Try this:
<appSettings>
<add key="parametro_de_lo_que_sea" value="valor_que_sea" />
</appSettings>
In your application you could use AppSettings.Get to get value:
string obtain_value = System.Configuration.ConfigurationManager.AppSettings["parametro_de_lo_que_sea"];
Good luck !!

Related

Compile App.config XML file along with console.exe application C#

Surely I have missed something simple here. I used the link below to help me create a configuration file which I can use in my console application.
Simplest way to have a configuration file in a Windows Forms C# Application
<configuration>
<appSettings>
<add key="setting1" value="key"/>
</appSettings>
</configuration>
I have built my console application, but can't see the app.config file anywhere. I'd like to edit this configuration file without recompiling.
Any idea on how to do this ? Basically, I'm passing the compiled console app to someone, and wont be providing the source code, so I have provided an .xml document which I hope they'll be able to edit after the console app has been compiled.
Thanks
You need click on your project in Solution explorer, select Add File option and choose app.config.
When you will rebuild solution file will by added configuration file like your_application.exe.config

Unable to open sqlite database file inside program

I'm trying to run a program I made (created a installer via Advanced Installer) but it keeps saying it can't open the database (sqlite) file. I've checked the paths and I think they match up, as it runs just fine inside Visual Studio. The App.config connection string is:
<connectionStrings>
<add name="db" connectionString="Data Source=qbc-members.sqlite;Version=3;" />
</connectionStrings>
Here is a screenshot of the location inside the project directory:
Here is the actual exception message:
Like I said, it runs as expected when debugging inside Visual Studio. When I check where the program was installed (C:\Program Files (x86)\user\qbc) it shows the sqlite database file inside the directory with the actual .exe file. So I'm really confused on why it can't find the file (as I tried looking up answers and the answer I think is relevant is that it can't find the file). Here is a screenshot as well of the actual program directory (once installed).
As you can see, the file is there. Any ideas on why it is throwing this exception?
Any help would be appreciated.
Thanks!
(Sorry if my question is unclear, please let me know and I'll try to add more info if needed).
The problem is most likely that application does not have write access to C:\Program Files (x86)\user\qbc directory. It's required because SQLite creates a lock file in the directory with database file.
Set directory write permissions for the user under which application is launched and the problem should be solved.

FIle path from app.config windows application

I always have doubt regarding how a path is formed whenever we run a windows app.
I have set a key like this in my app config
<add key="LogFilePath" value="..\Log\" />
When i run this from my local machine, it provides the path from where the windows app is run.
But when i run the same project from TFS, and when i try to create a file inside the Log folder , instead of the project mapped path it gives an entirely different path.
Can anyone tell me why this happens?
save the relative path in config and where you want to use it do it like this
string fullPath = Path.Combine(Application.StartupPath,configPath);
App Settings are very straight forward.
Add your properties to your App.Config app settings, e.g.
<appSettings>
<add key="LogFilePath" value="C:\Jaspreet_Files\LoadOrgInPortal.txt" />
</appSettings>
and read them, e.g.
var sqlConnectionString = System.Configuration.ConfigurationSettings.AppSettings["LogFilePath"];
I guess the problem is .. in the value:
<add key="LogFilePath" value="..\Log\" />
This seems to be a relative path, try to get full path first before writing and see where it is writing and where it should.

Problems while reading value from app.config file

I am making one windows application in c# in where i added one file as app.config file.I have written some code in that file as
<appSettings>
<add key ="FlagForArchiving" value="true"/>
</appSettings>
In 'program.cs' file i am reading this value as
ConfigurationSettings.AppSettings["FlagForArchiving"].ToString();
On local machine i can retrieve value from config file but whenever i am building that application and running on any other machine then I cant read the value from config file.I am trying to run my application on windows 7.Please help me.Thanks in advance.
app.config is renamed to <MyProgramName>.exe.config when you build. When your program runs it will look for that <MyProgramName>.exe.config file, not app.config.
You need to deploy the renamed file (<MyProgramName>.exe.config) along with your program.
In your case, you need to copy over OBViewer.exe, OBViewer.exe.config, and any other files that OBViewer.exe depends on (e.g. other .dll assemblies in your debug/release directory).
By the way, this renamed file is often commonly referred to as "app.config", even if it doesn't have the same filename.
and the app.config file exists on the other machine?
Before reading check if it exists
The exception you get says whats incorrect: "FileNotFoundException"
EDIT
here is the correct way!
if (File.Exists(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile))
{
MessageBox.Show(ConfigurationSettings.AppSettings["FlagForArchiving"].ToString());
}

Windows Service Config File C#

I've developed a windows service application using Visual Studio 2008 / C#.
I have an app.config file in the project. When installed, the app.exe.config file appears beside the executable but it appears not to be reading the values from it when I try to access them through ConfigurationManager.AppSettings.
Has it copied the config file elsewhere or is there some other problem I don't know about?
Thanks in advance,
Martin.
Edit:
The config file name is infact my_exe_file_name.exe.config, it looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="RuntimeFrequency" value="3" />
</appSettings>
</configuration>
and I am trying to read via:
ConfigurationManager.AppSettings["RuntimeFrequency"]
The debug value I continually see is '1' and not '3'. Am I doing something wrong here?
I located the error and it was related to file permissions. After installing the service, my local user account didn't have access to modify the app.exe.config file.
The tool I was using to edit was not informing me it was being denied access to save the file - that's notepad++ if anyone is interested - so I couldn't see that it wasn't saving over the old config file.
Solved now, thanks everyone.
Martin.
When you are in debug mode check and see what settings are in the my_exe_file_name.vshost.exe.config Also make sure you adjust this in the app.config file. Visual studio should update the final config file in your bin/debug folders.
Maybe you are updating the wrong config file. You should double check that using
System.Configuration.ConfigurationManager.OpenExeConfiguration(PATH_TO_CONFIG);
Generally for the Windows Services that I write, i drop the appName.exe.config file into C:\WINDOWS\system32\
Perhaps you have an older version in that directory, which is where your service is getting the value, even though you've updated the config file in your project.
App.config file should be renamed to your_exe_file_name.exe.config and placed near the exe file.
Is it possible that you have more than one instance of the RuntimeFrequency entry defined? The ConfigurationManager reads the file from top to bottom and processes each setting individually. Therefore, the last value of RuntimeFrequency that is defined in the file is the one it will use.
If you want to know for sure if your file is being used, I would simply remove or comment out any definition for RuntimeFrequency (copy/paste errors do happen) and wait to see an application error when ConfigurationManager attempts to reference an entry in AppSettings that does not exist.

Categories

Resources