开发者

Find a part of UNC path and put in a variable?

开发者 https://www.devze.com 2022-12-29 03:53 出处:网络
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.

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 --> \\o开发者_开发百科urfileserver\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


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();


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

Full docs here.


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

Looks something like:

var folderName = e.FullPath.Substring(e.FullPath.LastIndexOf("\\"));


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..


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消