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?

6 Answers
6

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.

Well I just like to have my stuff in a folder not flying around in the bin somewhere.
–
chobo2Jul 10 '10 at 18:34

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 ScottJul 10 '10 at 19:49

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
}

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.

With the second method you basically have to set it up to the right path each time you deploy it correct?
–
chobo2Jul 10 '10 at 18:34

@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_sJul 10 '10 at 19:27