FIle path from app.config windows application - c#

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.

Related

Windows form project is not showing any data from my embedded MS Access DB after installation

I am a beginner and I want to use MS Access DB inside my Windows form Project. My mdb file is showing data when I run it in debug mode. Once I create an exe and install it, it is not showing any data. The database is not password protected.
In App.config this is the connection string
<connectionStrings>
<add name="dbConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\mydb.mdb" providerName="System.Data.OleDb" />
</connectionStrings>
I assume that when you use "Data Source=|DataDirectory|" install & run the application,for example it looks for DB in C:\MyApp\Data\ folder. It should be C:\MyApp without additional \Data folder.
You need to call the AppDomain.SetData method to specify where the |DataDirectory| points to
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string relative = #"..\..\App_Data\";
string absolute = Path.GetFullPath(Path.Combine(baseDirectory, relative));
AppDomain.CurrentDomain.SetData("DataDirectory", absolute);
I had to add the .mdb file in my setup project.

Read part of appSettings from an external configuration file

I'd like to read a part of the appSettings of my console application from an external configuration file named, say, secrets.config, while the rest of it I would like to read from the app.config.
Presently, I have this set up but it seems like it isn't reading from secrets.config and it isn't even telling me about the read failure.
In my app.config
<appSettings file = "secrets.config">
<add key = "Foo" value = "Bar" />
</appSettings>
In secrets.config, which is in the same folder as app.config
<appSettings>
<add key = "Secret" value = "Tiger" />
</appSettings>
In my code
var secret = ConfigurationManager.AppSettings["Secret"];
// secret turns out to be null
It turns out that I was writing the path of the external file as the wrong path.
From the documentation on this page:
The path specified is relative to the main configuration file. For a Windows Forms application, this would be the binary folder (such as /bin/debug), not the location of the application configuration file. For Web Forms applications, the path is relative to the application root, where the web.config file is located.
I changed the path to the following at it worked:
<appSettings file = "..\..\secrets.config">
</appSettings>

Using Directory.GetFiles in my .net website

When I built my .net site, I retrieved files paths using
Directory.GetFiles(#"D:\project\images");
But when I deployed it on internet server, I have to change the link of this folder, can you help me how can I do that?? can I use relative link in Directory.GetFiles() or how??
Put the path in your web.config. You shouldn't be hard coding paths anyway. What if it changes in the future?
In your settings add:
<appSettings>
<add key="myPath" value="D:\project\images"/>
</appSettings>
...and then call it from your application:
var myPath = WebConfigurationManager.AppSettings["myPath"];
If you really want to go nuts, look into web.config transformations so that when you publish, your release configuration will be transformed and applied for you!
in the web.config have something like:
<configuration>
<appSettings>
<add key="ImagesFolder" value="\Images" />
</appSettings>
</configuration>
then in your ASP.NET C# code behind you can use:
var ImagesFolder = ConfigurationManager.AppSettings["ImagesFolder"];
var files = Directory.GetFiles(Server.MapPath(ImagesFolder));
mind that you need to add a reference to the System.Configuration assembly or you won't be able to add the using statement and access the ConfigurationManager.
in this way there are no hard coded values and you can write the value you want for that appsetting by editing the web.config file in the deployed folder on the web server.
The correct way to do this is by using Server.MapPath
Which maps the virtual path in your web app to the physical path in the server.

Is it possible to refer to home directory within appSettings in an App.config file?

I have this C# project in Visual Studio (2010), and I'd like to refer to a file in my home directory in the <appSettings> section of the project's App.config file. That is, I use this syntax:
<appSettings>
<add key="Database" value="sqlite:///C:\Users\arvek\test.db3" />
</appSettings>
Is it possible to refer to my home directory (C:\Users\arvek) via a variable instead of hardcoding it directly? F.ex.: value="sqlite:///$HOME\test.db3".
The ConfigurationManager won't automatically expand anything in the app settings, since they are just free-form strings, but you can do so manually. Use the ExpandEnvironmentVariables method of Environment, which will expand variables of the form %VARIABLENAME% according to the current environment. So:
<appSettings>
<add key="Database" value="sqlite:///%APPDATA%\database\test.db3" />
</appSettings>
string path = Environment.ExpandEnvironmentVariables(ConfigurationManager.AppSettings["Database"]);
The root path to your "home" directory is in the %USERPROFILE% variable, though %APPDATA% is the traditional place to put the kind of thing you're talking about. There is also %ALLUSERSPROFILE% for system-wide data (though in Windows 7 that actuallypoints to a special system-wide data folder, not the "Public" profile.)
I find it easier to just get the app's directory with this simple line of code
string appBasePath = AppDomain.CurrentDomain.BaseDirectory;
That's how I always refer to it.

Configuration from App.config isn't being pulled correctly

I'm trying to extract a URL I saved to the app.config file, but it's returning a blank string. Any ideas why?
string asdf = String.Format("{0}", ConfigurationManager.AppSettings["MemberUrl"]);
And the configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ForumUrl" value="http://www.dreamincode.net/forums/xml.php?showforum=" />
<add key="MemberUrl" value="http://www.dreamincode.net/forums/xml.php?showuser=" />
</appSettings>
</configuration>
If the app.config is part of a class library it probably isn't being copied to the bin folder properly (if at all).
The config file must be named <exefilename>.config for it to be picked up by the running application.
The App.config file in the application project (the one that produces an exe file, Console, WinForms, etc.) will copy and rename on deployment. Or if this is being executed from a web project it needs to go in the web.config.
Does this help?
All config information that your class library needs must be in the main projects App.config or web.config. In other words, if your app.config file is attached to the library it will NOT be read.
Go to the main application and add the appropriate keys/values to it's config file.
Sergio I just tried this is a console application and it works perfectly.
I would suggest that it's a class library; and not a main assembly that you have added your app.config file to.
When you do a build; look in the binary output folder Debug or Release and in there you should see a file named yourEXEfilename.config; if that file is not there then you will not get any output from the line of code you have above.
AppSettings will return a NULL string.
Hope this is of use
Kind Regards
Noel
there's no reason why that wouldn't work - do you have any other pertinent info ?
FYI, you dont need String.Format for what you're doing, the following is fine
string asdf = ConfigurationManager.AppSettings["MemberUrl"];

Categories

Resources