chobo2
chobo2

Reputation: 85765

How to get a path from a directory in a C# console application?

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?

Upvotes: 11

Views: 27021

Answers (6)

DotNetWala
DotNetWala

Reputation: 6610

In my console app, I started with the debug directory until i found the closest parent folder I wanted.

     static void Main(string[] args)
        {
            Console.WriteLine("Start");
            var debugDir = Environment.CurrentDirectory;
            DirectoryInfo di = new DirectoryInfo(debugDir);               
            var searchDir = "";
            while (!di.FullName.ToLower().EndsWith("Folder1"))
            {
                if(di.FullName.ToLower().EndsWith(":")) //if you went too far up as in "D:" then
                   break;
                di = di.Parent;
            }

           Console.WriteLine(di.FullName);
}

Upvotes: 5

marc_s
marc_s

Reputation: 754478

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.

Upvotes: 5

Antony Scott
Antony Scott

Reputation: 21998

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.

Upvotes: 6

Stephen Cleary
Stephen Cleary

Reputation: 456487

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

Upvotes: 1

Dirk Vollmar
Dirk Vollmar

Reputation: 176169

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
   }

}

Upvotes: 0

Anax
Anax

Reputation: 9372

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

Upvotes: 1

Related Questions