Sign in to follow this  

[.net] [Visual C#] Accessing Folders outside of the Bin folder

This topic is 4108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I am trying to store all my art and media and etc. in a folder, and is trying to store it outside the Bin folder. However, no matter how I try, I just can't seem to specify the path. Here's the directory structure Bin +- Debug +- Release Media From inside the program, I have tried @"..\..\Media\TransformTextured.fx" but it does not work. What other alternatives are there? Thanks in advance!

Share this post


Link to post
Share on other sites
Sometimes the root directory gets changed through the course of your application and isn't always start in your executable's directory. Instead, reference files explicitly by making your paths like this:

string path = Application.StartupPath + "myImagePath.bmp";

Share this post


Link to post
Share on other sites
The only thing I would say on top of Etherstar's reply is that Application.StartupPath doesn't always end with a trailing slash (I think it does if the startup location is something like "C:\" but I'm not 100% sure on that). Using System.IO.Path.Combine can take care of that problem for you though so your code could look something like...
System.IO.Path.Combine(Application.StartupPath, @"..\..\Media\TransformTextured.fx");

All the best,
ViLiO

Share this post


Link to post
Share on other sites
Application.StartupPath is good most of the time, but you'd have to reference System.Windows.Forms.dll if you want to use it in a console app.

Here's a solution that will always give you the full path to wherever your exe is located:

string basePath = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);



Environment.GetCommandLineArgs() will always have the full path to your exe as the first argument. Using Path.GetDirectoryName strips the file name from that.

Share this post


Link to post
Share on other sites

This topic is 4108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this