Aabirz
Aabirz

Reputation: 26

.NET c#: Why does Visual studio expect me to put my files in c:\...\myConsoleApplication\bin\Debug

I'm new to .NET programming and I get the following exception when trying to load an xml file that's located in my project's root. Can someone please explain to me why the program is looking for my file in the bin folder? Here's the exception:

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll Additional information: Le file'c:\users\u957974\documents\visual studio 2015\Projects\XMLINQConsoleApplication\XMLINQConsoleApplication\bin\Debug\contacts.xml' est not found.

Here is a screenshot of my project

Upvotes: 0

Views: 1169

Answers (5)

Chavi Gupta
Chavi Gupta

Reputation: 255

Bin folder is the working directory of any .Net project which you create.The Bin folder contains all the compiled assemblies. It has all the referenced Dlls and resources which the project is using. Since you have not specified the path for your Xml file it is trying to find it in the current directory and hence the exception.

You can try specifying the path like :

Xelement Xmldoc =  XElement.Load("../../contacts.xml")

Upvotes: 0

Ben
Ben

Reputation: 35643

Visual Studio doesn't know that your application needs that file to run. You have to tell it.

To do this, right-click the file in Solution Explorer. Choose "Properties".

Under Properties, find "Copy to Output Directory", and select "Copy if newer".

Upvotes: 3

Sir Rufo
Sir Rufo

Reputation: 19106

A relative file path is resolved from the current directory. The current directory when running the application from Visual Studio is the application directory.

But be aware that current directory can change at any time by several operations like OpenFileDialog

Upvotes: 0

Usha Chowdary
Usha Chowdary

Reputation: 25

Give path like this

   Xelement Xmldoc = Xelement.Load("~/COntracts.xml");

Upvotes: -1

Ivan Mladenov
Ivan Mladenov

Reputation: 1847

Because that is where the output of your program will compile. The resulted .exe file (assuming this is a console app) will be generated in bin/Debug/.

If you want to load the file from the root of your project, use a relative path like XElement.Load("../../contacts.xml")

Upvotes: 0

Related Questions