开发者

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

开发者 https://www.devze.com 2023-04-11 16:13 出处:网络
I have a pr开发者_运维问答oblem 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.?

I have a pr开发者_运维问答oblem 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?


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.


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


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


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")


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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号