views:

69

answers:

5

Hi

Say I have this file structure

Soultion-> Folder1 -> FileIwant.html

So this could be something like C:\Soultion\Folder1\FilterIwant.html

Now I need to read this file into my application. I can't just hardcode it since when I give it to someone else they might put it on F: drive or something.

Or when I create a msi file the path might be completely different. So how can I say maybe take

"Folder1\FilterIwant.html"

and use that to get the folder path regardless of where they put it?

Edit

I tried Path.GetFullPath but I land up in the bin/debug directory. But my file is not in that directory. I think it is a couple directories before. Also if I make a msi file will I have bin/debug directory?

+1  A: 

Path.GetFullPath

Edit

The bin/Debug path will not be present when you run your installed application (unless you specifically tell the installer to use that subdirectory, of course).

Stephen Cleary
Tried that. The path ends up being in the bin/debug so I am not sure what to do then.
chobo2
Updated answer to match updated question.
Stephen Cleary
+1  A: 

You need the help of System.Io.Path class:

GetFullPath: Returns the absolute path for the specified path string.

Edit:

You might also need the application directory - this is where your application will be installed:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);
Anax
A: 

You probably want to pass the full path as a command line argument. You can then get the argument using the args parameter of the Main method. To convert a relative path to an absolute one you can use Path.GetFullPath:

using System;
using System.IO;

public class CommandLine
{
   public static void Main(string[] args)
   {
       // The path is passed as the first argument
       string fileName = arg[0];

       // get absolute path
       fileName = Path.GetFullPath(fileName);

       // TODO: do whatever needs to done with the passed file name
   }

}

0xA3
+2  A: 

Why is a file which is used as part of your application not in the same folder as the application? It sounds to me like you should set the properties on that file to copy to the output folder when you do a build.

Doing that will make sure your file is in the bin\debug folder.

EDIT: either that or you should be placing your files in one of the special folders, app data or my documents spring to mind.

Antony Scott
Well I just like to have my stuff in a folder not flying around in the bin somewhere.
chobo2
that's fine, and there are valid reasons for doing that. particularly in a locked down environment. so, i would suggest looking at giving your files a home in the AppData folder somewhere.
Antony Scott
+1  A: 

When Visual Studio compiles your project, it will be putting the output into the bin\debug directory. Any content files that you want to reference must also be copied to those locations, in order for your app residing in that directory to be able to read that file.

You have two choices:

  • either you set the Copy to Output Directory property on your FilterIwant.html to Copy if newer; in that case, if the file has changed, it will be copied to the output directory, and you should be able to reference it and load it there

or

  • you just define a path in your app.config, something like DataPath, and set it to your folder where the file resides. From your app, you then create the full path name for that file as Path.Combine(AppSettings["DataPath"], "FilterIwant.html") - with this approach, you become totally independant of where the file really is and you don't need to move around anything. Also: this gives you the opportunity to create an admin/config utility for your users later on, so that they can pick any directory they like, and your app will find those files there.
marc_s
With the second method you basically have to set it up to the right path each time you deploy it correct?
chobo2
@chobo2: you'd have to set it up once for each installation, yes. But having a config entry also gives you the ability to let the end user change that on their own, using e.g. a config utility. If you hardcode the behavior into your code, you don't have that possibility
marc_s