In my Visual studio 2008 project, I've added app.config file where I store some app-data in xml format.
I read this data in code like this:
string somedata = ConfigurationSettings.AppSettings["somedatakey"].ToString();
when I start application in Visual studio, it works. But if I try to run the exe file (release or debug) I get error (if i debuf it it breaks on the line above):
Object reference not set to an
instance of an object.
The file app.config is not inside folder.
Is the app.config file in the same folder as your exe? If not, copy it there.
Starting debugging in visual studio builds everything, and copies the output (including app.config) to the output folder, starting it from there.
#Jullin: When you run project from visual studio editor by pressing F5 then CLR pick app.config file to read data but when you want to run project from .exe (bin/debug or bin/release) then clr read applicationName.exe.config, which you must have within your debug or release or any folder from where you access you applicationName.exe.
Like i have a window application named "WindowsFormApplication", when i build it successfully in release folder i have WindowsFormApplication.exe and WindowsFormApplication.exe.config and some other files. so make sure you release project successfully and your release folder must contain files.
While Running in from the Exe make sure that u have added app.config should be in the same directory
Related
When I build a windows application. It does not work because it cannot read my app.config file. I looked in the bin directory and it does not have the appname.exe.config file. If I manually copy over the app.config and rename it the appname.exe.config the application will work..but when I build the project this file is never created automagically. What am I missing? Why is my project not creating it? I have looked through the whole folder structure and there is no other config files.
Everyone here is giving you false information I think. I think #bowlturner had it right. According to Microsoft, you do need to set the app.config's Copy to output directory property to either Copy Always or Copy if newer. From Microsoft's MSDN Site:
When you develop in Visual Studio, place the source configuration file for your app in the project directory and set its Copy To Output Directory property to Copy always or Copy if newer. The name of the configuration file is the name of the app with a .config extension. For example, an app called myApp.exe should have a source configuration file called myApp.exe.config.
Visual Studio automatically copies the source configuration file to the directory where the compiled assembly is placed to create the output configuration file, which is deployed with the app.
The correct settings are:
Build Action = None
Copy to Output Directory = Do not copy
and very important, the project file needs to contain this element:
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
The only thing that worked for me was to delete bin\ and obj\ entirely. Rebuild and the file is created fine.
Look on App.config properties, should be:
BuildAction: None
CopyToOutputDirectory: Do not copy
Custom tool and custom tool namespace should be empty
Also try to Rebuild project. Right click on project -> Rebuild
Assuming you're using Visual Studio click your config file in the Solution Explorer and and in the Properties panel set Copy To Output Directory to something other than Do Not Copy.
My solution has two exe apps inside. Console application (Console.exe) and wpf application (MyUI.exe). Wpf application is a startup project, and console one is a small installation tool. Both have app.config file which contains db info. I have problem with this config file after I build the solution. In bin folder I have my wpf app and config file with the same name, e.g.:
MyUI.exe and MyUI.exe.config.
But there is no config file for console application. Is there any setting I should set to make it right?
When I set build action to content and copy to output... to Copy always then I have "App.config" file in bin directory, but there is no "Console.exe.config".
There is no such thing as a solution output folder - each project has it's own output folder, in which the App.config will be placed (after it's renamed to [exe_name].config).
I have compiled a simple 'Hello World' program. The program is successfully compiled without any errors. I can see a working executable in bin folder. But the Release folder of the project is totally empty.
I don't know if there is any settings problem or any problem in building. Apparently, I don't see any error.
You can change your build mode in top of your Visual Studio:
Dependently on which mode you build your solution, Visual Studio will generate .exe in either bin\Debug folder or bin\Release folder.
Make sure you're building the release version, not the debug version. By default, VS will build a Debug build, which will go into the bin\Debug folders.
For details, see How to: Set Debug and Release Configurations.
More expanded explanation:
Your output directory of executable file is specified by default to [your_project_directory]\bin\Debug for Debug build mode or to [your_project_directory]\bin\Release for Release build mode
your_project_directory - place where is created your project (there is located *.csproj file)
To switch between default directories Debug/Release use solution proposed here.
But if you want specify your own output directory, follow these steps:
Go to Project -> [MyProjectName] Properties... (or type Alt+F7) and in properties window switch to Build tab (from left panel)
From Configuration combobox select proper build mode (in your case Release) or select: "All Configurations" - the same options for both build modes
At the end in output path textbox choose folder where you want store executable file (and other created during buil of application)
Screenshot from properties window:
This solution can be useful at least in two cases:
When are you learning and you want not waste space on your disc, then you can specyfy outbut file for all your project to one tmp directory and clean it in some period of time from not necessary files
When your start work with real "huge project" that include many e.g. .csproj projects your can create Runtime directory to storage all your executable files and files created during build of application
I hope that this description will be helpful.
I have a clickonce app that needs the app.config file deployed.
In Visual Studio, if I right click on the app.config file and select properties then change the Build Action to Content, the app.config.deploy file gets created, but there is no app.exe.config.deploy file.
I really don't even need the app.exe.config.deploy file and I don't know why Visual Studio (2010) includes it.
At this point, I click on the properties for the clickonce .csproj file, click on the publish tab and click on Application Files. "app.config" and "app.exe.config is set to "Include (auto)". But I don't have the option to disable the app.exe.config.
The reason I need app.config deployed is because I'm using the following code:
var str = ConfigurationManager.AppSettings["oem"];
I'm deploying multiple versions of this with an external build script. The build script swaps out different versions of the app.config for each different published version of the code.
Bottom line question. Is there a way to either:
Get the app.exe.config to deploy with the rest of the publish
Only get the app.config file to deploy (without the app.exe.config)
If I add say a text file to the project and set it's deployment type to "content" as specified above it gets included.
I have an annoying error that's been popping up, and I don't know where it is coming from. The error is:
Error 31 Unable to copy file "app.config" to "bin\Debug\Framework.Tests.dll.config". Could not find file 'app.config'. Framework.Tests
The problem is, I don't have a bin\Debug folder anywhere, and it does not say from where it is trying to copy app.config. Double clicking on the error doesn't bring me to any code where it is trying to copy, so that doesn't help me either. So I don't know where I should make the app.config.
How can I find this out?
You have added a reference to a file called app.config or Visual Studio did this for you. The file does not exist on the disk, presumably because after adding the reference you later deleted the file.
Search for the missing file in your Solution Explorer, and either delete the reference or create the file.
It is most likely in the root folder of one of your projects, and should have a yellow triangle warning icon showing that the file is missing.
In an MSTest project the app.config is the one that you would like to provide any calls to the ConfigurationManager class.
It resides in the root of your test project.
The bin\debug folders will be created once the project compiles for the first time. Show all files in the solution explorer should help as they are not (and should not) be included in the project.
HTH
You probably do have a bin\Debug folder beneath your project folder, being the build target folder created by Visual Studio when you build your project for the Debug configuration.
My guess is that something (a test framework perhaps) still has the DLL file loaded, so Visual Studio can't delete and replace the existing Framework.Tests.dll.config file with the contents of your app.config. [Note: the project build action for app.config files is to copy it to the target folder renamed to match the executable with an extension of .config appended.]