1

I need to reference a few .exe files in my WPF application, which are stored in a folder (call it appFolder) shown in the Solution Explore in Visual Studio. How can i refer to this folder relatively (like Server.MapPath(virtualPath); in MVC)?

After reading this post, i tried Path.GetFullPath("appFolder"), but this maps to something like

C:\Users\....\MyProject\bin\Debug\appFolder

while the folder resides at

C:\Users\....\MyProject\appFolder 

without the \bin\Debug.

If something like this is my only option, is it safe to manually construct the real path by ripping off \bin\Debug? Thanks!

CLARIFICATION I was not asking how to "rip off" \bin\Debug; rather, I asked if it's safe to do so, which was answered in svinja's comments.

Community
  • 1
  • 1
totoro
  • 3,257
  • 5
  • 39
  • 61
  • 2
    Why would you want a dependancy on the solution directory structure? That will not be there once you deploy. A user will not have the whole solution with the source code on their machine, just the bin/Release part or something else. If the files are needed, they should be deployed with the application, so you should access the relative paths where these files are deployed. Rethink what you're trying to do, instead of using ``..`` to "go back" to the solution directory. Click a file in the solution explorer, press F4 and look at the options. Is "Copy to output directory" what you really want? – svinja Nov 05 '14 at 08:16
  • @svinja thanks for the warning! you said "you should access the relative paths where these files are deployed". how do i know the relative paths of the deployed files? – totoro Nov 05 '14 at 08:28
  • Hey @green. Your Path.GetFullPath() will start from the current directory of the executable calling it. svinja is right that your app is suspiciously dependent on appFolder being two levels up because of the bin and debug folders from your solution folder (unless you're the only user of your WPF app and don't plan to distribute it, and in that case there's nothing wrong with that :) ). As for the paths of your executables, if ever you plan to roll it out, it's clearly your choice where to put it. – Tyress Nov 05 '14 at 08:38
  • 1
    Do you want to reference the files in XAML or a cs-file? Is the appFolder in the same assembly as the code? "If an application data file must always be available to an application, the only way to guarantee availability is to compile it into an application's main executable assembly or one of its referenced assemblies." [from MSDN](http://msdn.microsoft.com/en-us/library/aa970494(v=vs.110).aspx) – Alex Nov 05 '14 at 20:15
  • @Alex i want to reference it in cs-files. sorry, I am new to this, not sure what you mean by "same assembly as the code". i can see the `appFolder` from "Solution Explorer" in visual studio. it's under `resources/appFolder`. i also have styles in `resources/Styles.xaml`. – totoro Nov 06 '14 at 01:39

1 Answers1

0

How about using String.replace to replace "\bin\Debug" with String.Empty ?
However I don't deny what @svinja has suggested.