Author Topic: Get Parent Folder Name  (Read 2770 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
Get Parent Folder Name
« on: September 07, 2010, 05:18:23 PM »
I have done this before but cannot remember how I did it
I thought I did with a method from the Path class but I can find it. I looked in the DirectoryInfo but it will do it for active file. I am passing in a string.

Ex:
C:\BlahBlah\YadaYadaYada\Swamp\ThisIsTheOneWant\File.dwg

How can I extract just "ThisIsTheOneWant" by passing in an string
I thought there was method for that or do I need to do Split with "\" as the delimiter or some other way?

Is asking for Parent Folder the correct terminology?
The parent folder being the folder the file is contained in.

LE3

  • Guest
Re: Get Parent Folder Name
« Reply #1 on: September 07, 2010, 05:54:27 PM »
have not needed, but looks like Directory class has a GetParent method

maybe?

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Get Parent Folder Name
« Reply #2 on: September 07, 2010, 06:06:14 PM »
Look like you can do something like

new FileInfo( <YourPath> ).Directory.Name
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Get Parent Folder Name
« Reply #3 on: September 07, 2010, 06:25:25 PM »
Thanks le & T.Willey this is the only one I could get to work so far
Code: [Select]

                string groupName;
                FileInfo groupN = new FileInfo(lvi.ImageKey.ToUpper());
                groupName = groupN.Directory.Name.ToString();

It seems what le posted should work but for some reason it gives me the full path. It might be in the context I am using it why it does not.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Get Parent Folder Name
« Reply #4 on: September 07, 2010, 06:28:14 PM »
No need to convert it to a string, as the Name property returns a string.  You shouldn't need to change the path to upper case either.  Have you had it error before?

Glad you got it working.  You're welcome.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Get Parent Folder Name
« Reply #5 on: September 07, 2010, 06:38:00 PM »
This worked for me.

MessageBox.Show( new FileInfo( path ).Directory.Name );
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE3

  • Guest
Re: Get Parent Folder Name
« Reply #6 on: September 07, 2010, 06:41:47 PM »
Just tried and works:

Code: [Select]
           
string fileName = @"C:\LicenseServer\install\web.config";
string sdir = Directory.GetParent(fileName).Name;

returns: "install"

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Get Parent Folder Name
« Reply #7 on: September 07, 2010, 06:43:58 PM »
here's a rough one I used to get me out of trouble in the past ... just for reference
Code: [Select]
//get the current drgs' containing folder:
string folderpath = doc.Name;
int lastslash = folderpath.LastIndexOf("\\") + 1;
folderpath = folderpath.Remove(lastslash,folderpath.Length - lastslash);
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Get Parent Folder Name
« Reply #8 on: September 07, 2010, 07:09:18 PM »
Quote
It seems what le posted should work but for some reason it gives me the full path. It might be in the context I am using it why it does not
Let me requote that to
Quote
For some reason if I do it wrong and leave off the .Name it gives me the full path. If I do it right like le posted it works perfectly
Sorry about that and thanks again le

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Get Parent Folder Name
« Reply #9 on: September 07, 2010, 07:20:07 PM »
Have you had it error before?

No because I am saving the file name as the image key for a ListVew control so when I double click it I can use the image key to open the database and with the code posted I am using it for setting the group name.
And T.Willey thanks for calling out the redundant ToString

It is the beginning of Standard Detail Library Manager
« Last Edit: September 07, 2010, 07:24:25 PM by fro2001 »