Click here to Skip to main content
15,944,733 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I want to get the full path by passing the folder name

Means Ex: if my folder name is "test" inside the "C:\Home\test" and if I will pass the only "test " then it should reply "C:\Home\test"

Use the Path.GetFullPath Method[^]:
using System.IO; // add this at the top of your code file

string fullpath = Path.GetFullPath("test");

Hope this helps.
Share this answer
ridoy 8-Sep-13 13:27pm    
very good link,+5.
Thomas Daniels 8-Sep-13 13:28pm    
Thank you!
Sergey Alexandrovich Kryukov 8-Sep-13 15:05pm    
That's not OP is asking about. OP knows the directory, separately, and simple file name, separately.
I put appropriate answer, please see.
BillWoodruff 9-Sep-13 1:09am    
+5 Very possible this is all the OP needs, as long as they are absolutely certain the current working Directory contains the folder "test." See my comment on how "dumb" Path.GetFullPath() is, in my "devil's advocate" solution, below.
Thomas Daniels 9-Sep-13 12:50pm    
Thank you!
This is not exactly what Solution 1 recommends. Rather, you need to use System.IO.Path.CombinePath:[^].

Alternatively, you can do it literately:
string fullPath = string.Format(
    System.IO.Path.PathSeparator, // this is the key: don't hard-code '\'; it would be not platform-portable

Good luck,
Share this answer
phil.o 8-Sep-13 19:20pm    
I read the question several times, and it seems that OP was really asking how to get a full directory path given only the directory name :)
Or the question has been edited... or it's too late now and I have to quit...
Sergey Alexandrovich Kryukov 9-Sep-13 0:50am    
Please read the OP's example in the last line, and you well see that my interpretation of the question is correct.
However, if OP replies, we can certainly provide additional advice for this simple matter.
BillWoodruff 9-Sep-13 0:43am    
Excellent point made in your code by handling platform-portability issues !
Sergey Alexandrovich Kryukov 9-Sep-13 0:49am    
Thank you, Bill.
Thomas Daniels 9-Sep-13 12:54pm    

I've read your comment, and you said that the OP knows the folder name and the sample file name seperately. I understood it as the OP only has only one folder name, and he wants to get the full path from it, but indeed, it could be that he means that he wants to combine two paths.

The OP should pick the correct answer, because he is the only one who understands 100% what he means.
This "began life" as a comment, but I think what I have to say warrants posting this as a solution. I consider it quite possible that my esteemed colleagues Sergey, and ProgramFox, have adequately answered this question (I've upvoted both their responses). But, in the spirit of "trust nothing, question everything," I'll pose a possible confounding scenario: because I wonder, given the trivial nature of the solution if the folder "test" is contained in the current working Directory, if the user might be asking a different question.

In any case I hope this response makes some contribution to the OP's knowledge of directories in .NET: if not, please do: vote it down.

Consider that a very important piece of information may be missing here: when the OP "passes in" a folder name do they:

a. already have the path to the folder (or desktop, or wherever) the folder passed-in is contained in "captured" in some variable ?

... or ...

b. do they know, for certain, that the current working Directory of their application contains the folder, "test." ?

If they already have the path "captured," or, are certain the path is contained in the current working Directory: then this problem becomes trivial: just use the path you have "captured," and append the folder name to it. Or, just use Path.GetFullPath() to prepend the current working Directory

However, if the running program doesn't "know" where the folder "test" is located, and the running program is not "sure" if the folder "test" is contained in the current working Directory: this becomes an entirely different problem because:

Path.GetFullPath() is a "dumb" function: you can hand any file, or folder, name to it, and it will return the current working Directory path prepended to it ... whether or not that folder is contained in the current working Directory path !

So, if the OP is asking this question in the broader sense implied by the scenario I've outlined above, then Path.GetFullPath() may return a "false positive."

To handle this alternate scenario:

1. first: use Path.GetFullPath("test") to get a fully qualified filepath.

2. then: determine if the fully qualified filepath exists.

3. if it exists:

a. if the filepath exists, and you are sure the one instance of the folder "test" you want is in the current working Directory: you're done.

b. if the filepath exists in the current working Directory, but you think there may be other folders named "test" in other locations, and, you want to choose from them, if there are: then you've got to do some kind of a search, get the results, and, if there are multiple folders named "test:" select which one(s) you want.

4. (false positive) if the folder "test," however, does not exist in the current working Directory: then you have to do a search of some type, and you have to consider the "scope" of that search (the whole network ? all hard-drives ? only a certain "root" Directory ?).
Share this answer
Thomas Daniels 9-Sep-13 12:56pm    

You are correct, the OP didn't mention WHERE the "test" folder is stored exactly. He didn't mention whether it's stored on the current directory, or just anywhere at the disk.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900