views:

35

answers:

5

I am trying to peel off the last part of a unc path that is being passed and put it in a variable to use in a method further down the line.

Example path would be --> \\ourfileserver\remoteuploads\countyfoldername\personfoldername

How do I peel just the countyfoldername out of that?

I had thought to try

var th = e.FullPath.LastIndexOf('\\');
        var whichFolder = folderPath.Substring(th);

but that is an escape character and it doesn't like @ either.

Is this even the right direction?


I think I have confused some of you. LastIndexOf doesn't work because I need the countyfoldername which, in my example, occurs 3/4 of the way through.

Also, I need the countyfoldername stored in a variable, not the file name itself.

To give some context, I have a FileSystemWatcher that runs in a service. It was monitoring a single folder path and emailing when a file was created there. Now I need to modify it. There are now 4 county folders at that folder path and I need to send an email to a different email address depending on where a file is created.

I can use a simple switch statement if I can figure out how to get the county folder name reliably.

Thanks

+1  A: 

Did you try:

var myCounty = e.FullPath.LastIndexOf("\\"); 

Update: In order to get the country folder name, just trim off the number of characters found from the county look up, then do another last index of..

Chris Lively
single of double quotes there? just checking as don't char's use single? Thanks
Refracted Paladin
doesn't lastindexof return an integer? wouldn't he need to grab the substring starting at the lastindexof?
jsmith
yes my plan was to do something like this --> `var th = e.FullPath.LastIndexOf('\\'); var whichFolder = folderPath.Substring(th);` unless there was a better way...
Refracted Paladin
+1  A: 

You need to create a substring from LastIndexOf("\")

Looks something like:

var folderName = e.FullPath.Substring(e.FullPath.LastIndexOf("\\"));
jsmith
+1  A: 

string folder = System.IO.Path.GetFileName(fullpath)

Full docs here.

dpurrington
A: 

I think you should split.

string[] Sep= {"\\"}

string[] Folders;
Folders= folderPath.Split(NewLine, StringSplitOptions.RemoveEmptyEntries);

You can traverse the array and hace full control over the string.

Daniel Dolz
+2  A: 

Actually, you should use the Uri Class and break it into segments.

    Uri uri = new Uri(@"\\ourfileserver\remoteuploads\countyfoldername\personfoldername");
    Console.WriteLine(uri.Segments[3]); // personfoldername
    Console.ReadLine();
mbcrump