Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 662
chobo2 Avatar asked Jul 09 '10 22:07

chobo2


People also ask

How do I find the path of a directory?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I get to the file path?

Search File Explorer: Open File Explorer from the taskbar or select and hold the Start menu (or right-click), select File Explorer , then select a search location: To quickly find relevant files from your PC and the cloud, search from Home. To find files stored inside a folder, search from a folder like Downloads.

What is the path of a directory?

A path is a slash-separated list of directory names followed by either a directory name or a file name. A directory is the same as a folder.

What is the command to display directory paths?

The pwd command displays the full, absolute path of the current, or working, directory.


1 Answers

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.

like image 126
Antony Scott Avatar answered Oct 09 '22 19:10

Antony Scott