views:

73

answers:

4

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);
}}
+7  A: 

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.

Jon Skeet
+1: but a link would be nice :)
CAbbott
@CAbbott: I was getting there :)
Jon Skeet
@CAbbott http://www.google.co.nz/search?q=Directory.GetParent
Neal
@Neal: I know the link, I was just prodding Mr. Skeet to include one in his post.
CAbbott
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
@CAbbott: For next time, feel very free to add the links yourself. I promise not to bite :)
Jon Skeet
+1  A: 

Why not just handle this directly using the System classes?

string folder = Environment.GetFolder(Environment.SpecialFolder.LocalApplicationData);
Reed Copsey
A: 

I would use the DirectoryInfo class.

DirectoryInfo tempDirectory = new DirectoryInfo(Path.GetTempPath());            
DirectoryInfo tempDirectoryParent = tempDirectory.Parent;
Austin Salonen
+1  A: 

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.

Jay
Thank you Jay. I'll have to check that book out. I've been meaning to look into Regular Expressions.
JimDel