Reputation: 26
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.
Upvotes: 0
Views: 1169
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
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
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
Reputation: 25
Give path like this
Xelement Xmldoc = Xelement.Load("~/COntracts.xml");
Upvotes: -1
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