0

How can I use IndexOf with SubString to pick a specific Character when there are more than one of them? Here's my issue. I want to take the path "C:\Users\Jim\AppData\Local\Temp\" and remove the "Temp\" part. Leaving just "C:\Users\Jim\AppData\Local\" I have solved my problem with the code below but this assumes that the "Temp" folder is actually called "Temp". Is there a better way? Thanks

if (Path.GetTempPath() != null) // Is it there?{
tempDir = Path.GetTempPath(); //Make a string out of it.
int iLastPos = tempDir.LastIndexOf(@"\");
if (Directory.Exists(tempDir) && iLastPos > tempDir.IndexOf(@"\"))
{
    // Take the position of the last "/" and subtract 4.
    // 4 is the lenghth of the word "temp".
    tempDir = tempDir.Substring(0, iLastPos - 4);
}}
2
  • Just to clarify, you want to trim off the last sub-directory in a path regardless of if it's temp or not?
    – Drew
    Apr 13, 2010 at 19:20
  • Yes, its going to be either TEMP or possibly TMP
    – JimDel
    Apr 13, 2010 at 19:22

4 Answers 4

7

The better way is to use Directory.GetParent() or DirectoryInfo.Parent:

using System;
using System.IO;

class Test
{
    static void Main()
    {
        string path = @"C:\Users\Jim\AppData\Local\Temp\";
        DirectoryInfo dir = new DirectoryInfo(path);
        DirectoryInfo parent = dir.Parent;
        Console.WriteLine(parent.FullName);
    }    
}

(Note that Directory.GetParent(path) just gives you the Temp directory, as it doesn't understand that the path is already meant to be a directory.)

If you really wanted to use LastIndexOf though, use the overload which allows you to specify the start location.

4
  • @CAbbott: I was getting there :)
    – Jon Skeet
    Apr 13, 2010 at 19:25
  • @Neal: I know the link, I was just prodding Mr. Skeet to include one in his post.
    – CAbbott
    Apr 13, 2010 at 19:29
  • Nope, didn't need to use LastIndexOf at all. It was just readily available in my SMALL list of tools I keep in my head. I wasn't aware of GetParent. This works perfectly for me and is MUCH more elegant. Thank you so much!
    – JimDel
    Apr 13, 2010 at 19:32
  • @CAbbott: For next time, feel very free to add the links yourself. I promise not to bite :)
    – Jon Skeet
    Apr 13, 2010 at 19:34
2

Why not just handle this directly using the System classes?

string folder = Environment.GetFolder(Environment.SpecialFolder.LocalApplicationData);
1

The other answerers have shown the best way to accomplish your goal. In the interest of expanding your knowledge further, I suggest that you look at regular expressions for your string matching and replacement needs, in general.

I spent the first couple of years of my self-taught programming career doing the most convoluted string manipulation imaginable before I realized that someone else had already solved all these problems, and I picked up a copy of Mastering Regular Expressions. I strongly recommend it.

One way to strip off the last directory is with the following regular expression:

tempDir = Regex.Match(tempDir, @".*(?=\\[^\\]+)\\?").Value;

It may look cryptic, but this will actually remove the last item from the path, regardless of its name, and regardless of whether there is another \ at the end.

1
  • Thank you Jay. I'll have to check that book out. I've been meaning to look into Regular Expressions.
    – JimDel
    Apr 13, 2010 at 19:55
0

I would use the DirectoryInfo class.

DirectoryInfo tempDirectory = new DirectoryInfo(Path.GetTempPath());            
DirectoryInfo tempDirectoryParent = tempDirectory.Parent;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.