Febin J S
Febin J S

Reputation: 1368

How to get path of a file which we include in our solution folder?

I have a problem where i have to get the path of a xml file which i have included in my solution. Can any body how to get the path of that file.?

What i have tried is

string path = Directory.GetCurrentDirectory() + "/test.xml";

But this does not give me the correct file path. Can anybody provide me the details to find the path?

Upvotes: 0

Views: 7362

Answers (6)

KV Prajapati
KV Prajapati

Reputation: 94653

It depends upon the location of file. The method Directory.GetCurrentDirectory() returns a path of executable (bin\debug)

Upvotes: 0

Sam Casil
Sam Casil

Reputation: 948

Try this..

String path = Application.StartupPath + "\test.xml";

If your xml was inside in the BIN folder, you can use that code on getting your xml.

Upvotes: 1

Erik Dietrich
Erik Dietrich

Reputation: 6090

I would be sort of leery of this requirement if I were you. Conceptually, your solution directory and solution in general is the "building materials" and your compiled, deployable code your finished "building" that you'd give to a customer. What you're asking for here is like the builder of a house saying "What's the best way to cement my hammer into the floor of the basement?" Someone could probably give you a good answer, but is correctly applying the solution a good idea?

To be more concrete about it, why does your run-time, executable code depend on your pre-build/build time solution structure? I'd suggest that what you actually probably want to do is have a build event that copies the XML from your solution folder to the target directory where your target executable is built and deployed. If you do that, then you can just use your "CurrentDirectory" approach in the code without worrying about how the solution is structured. The build events use the command line syntax and have available 'macros' for your solution directory, so you'd have one that's something like "xcopy $(SolutionDir)test.xml $(BuildDir)"

(My apologies if you're already aware of this and you have some particularly specific requirement for having code that traverses its own solution structure).

Upvotes: 0

Maheep
Maheep

Reputation: 5605

If your parameter accept a URI you can use something like this

new Uri("/<proejct or assembly name>;component/<folder path>/<filename>")

For example my proejct foo.bar has a folder named resources in which I have a file say dummy.xml I can use

new Uri("/foo.bar;component/resources/dummy.xml")

Upvotes: 0

Jared Peless
Jared Peless

Reputation: 1120

If it is in the solution folder then it won't be in the current directory of the application (the proper bin folder depending on build configuration) unless you make sure it is a Content type and is copied to the build folder

Upvotes: 0

Andrey Atapin
Andrey Atapin

Reputation: 7955

You need put files in a project's root folder. Also, try to correct slash: ... + "\\test.xml"

Upvotes: 0

Related Questions