Katalogning ba'zi qismlarini qanday qilib olish mumkin? C #

Misol uchun,

foreach (var imageFile in Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories))
{
    //add to list
}

Output of imageFile = C:\Users\Documents\MyWeb\Slide\Main\slider2.png

I need to get only \Slide\Main\slider2.png

Buning oson yo'li bormi?

0
Xo'sh, qaysi qoidadan foydalanish kerak? Buning uchun MyWeb ga asoslangan yo'lni ajratish mumkin, ammo boshqa kataloglar haqida nima deyish mumkin? U ikkiga bo'lingan umumiy joy bormi?
qo'shib qo'ydi muallif rory.ap, manba
@DavidG - Buni hali etarli ma'lumotga ega bo'lmaganimizda, uni qanday qilib dupe qilib yopishingiz mumkin?
qo'shib qo'ydi muallif rory.ap, manba
Bu ushbu qismning qanday aniqlanishi kerakligiga bog'liq. Har doim yo'lda to'rtta jild yoki boshqa biror narsa bormi?
qo'shib qo'ydi muallif Andy Korneyev, manba
Qaysi qismini bekor qilishga qaror qilasiz? Nima uchun faqat rasm yo'lidan katalog yo'lini olib tashlamaslik kerak? masalan, imageFile.Remove (dir);
qo'shib qo'ydi muallif Panagiotis Kanavos, manba
@megatron nth slashning indeksini topib, qolgan qismini bilan toping .Substring()
qo'shib qo'ydi muallif Panagiotis Kanavos, manba
@megatron nth slashning indeksini topib, qolgan qismini bilan toping .Substring()
qo'shib qo'ydi muallif Panagiotis Kanavos, manba
@megatron Barcha tasvir yo'llarini C: \ Users \ Documents \ MyWeb papkasiga nisbatan qilishga harakat qilyapsizmi?
qo'shib qo'ydi muallif DavidG, manba
@megatron Sizga qanday fayllar kerakligini bilsangiz, nega butun katalogni qidirishingiz kerak? Men biroz murakkabman.
qo'shib qo'ydi muallif Jordy, manba
@megatron Sizga qanday fayllar kerakligini bilsangiz, nega butun katalogni qidirishingiz kerak? Men biroz murakkabman.
qo'shib qo'ydi muallif Jordy, manba
@DavidG yo'q, bu o'zgaradi. faqatgina \ Slide \ Main-dan jild o'zgarmaydi.
qo'shib qo'ydi muallif megatron, manba
@AndyKorneyev har doim bu \ Slide \ countlarni ajratadi? chunki \ Slide \ papka nomi o'zgartirilmaydi.
qo'shib qo'ydi muallif megatron, manba
@PanagiotisKanavos faqat faylning oxirgi papkasiga ikkinchi kerak
qo'shib qo'ydi muallif megatron, manba
@ rory.ap har doim ham \ Slide \
qo'shib qo'ydi muallif megatron, manba

9 javoblar

Siz ma'lum bir jildni qidirayotganingizdan so'ng buni amalga oshirishingiz mumkin:

var imageFile = @"C:\Users\Documents\MyWeb\Slide\Main\slider2.png";
var subFolder = @"\Slide\Main";

var relativePath = imageFile.Substring(
    imageFile.IndexOf(subFolder, StringComparison.OrdinalIgnoreCase));
1
qo'shib qo'ydi

Siz ma'lum bir jildni qidirayotganingizdan so'ng buni amalga oshirishingiz mumkin:

var imageFile = @"C:\Users\Documents\MyWeb\Slide\Main\slider2.png";
var subFolder = @"\Slide\Main";

var relativePath = imageFile.Substring(
    imageFile.IndexOf(subFolder, StringComparison.OrdinalIgnoreCase));
1
qo'shib qo'ydi

Nima uchun string.Split ("\") dan foydalanmasangiz va u sizga ushbu yo'lning barcha qismlarini beradi va keyin siz istagan joydan qolganlarni birlashtira olasiz yoki Zalomon aytganidek Regular Expressionn dan foydalanishingiz mumkin

0
qo'shib qo'ydi

Men buni birinchi marta yozgan odamga pul to'lashni xohlayman, lekin men bu linkni yo'qotib qo'ygandim.

Shaxsiy yordamchi to'plamimdan:

    /// 
/// Generates a Relative Path for targetPath, from basePath ///
 
    /// 
The Directory to start from.
    /// 
Full Path to file or folder that is to be made relative
    /// The relative location of targetPath, from basePath
    /// Tested in Util.Tests.IO.PathUtilsTests
    public static string MakeRelativePath(string basePath, string targetPath)
    {
        if (basePath == targetPath)
            return "";

        if (!basePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            basePath += Path.DirectorySeparatorChar;

        if (String.IsNullOrEmpty(basePath)) throw new ArgumentNullException("basePath");
        if (String.IsNullOrEmpty(targetPath)) throw new ArgumentNullException("targetPath");

        var fromUri = new Uri(basePath);
        var toUri = new Uri(targetPath);

        if (fromUri.Scheme != toUri.Scheme) { return targetPath; }//path can't be made relative.

        var relativeUri = fromUri.MakeRelativeUri(toUri);
        var relativePath = Uri.UnescapeDataString(relativeUri.ToString());

        if (toUri.Scheme.Equals("file", StringComparison.InvariantCultureIgnoreCase))
        {
            relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
        }

        return relativePath;
    }

Foydalanish:

var output = @"C:\Users\Documents\MyWeb\Slide\Main\slider2.png";
var basePath = @"C:\Users\Documents\MyWeb\";

var rel = MakeRelativePath(basePath, output);
0
qo'shib qo'ydi
Bu shaxs bormi?
qo'shib qo'ydi muallif Sinatr, manba
Ha! Buni birinchi marta olganimga o'xshaydi.
qo'shib qo'ydi muallif Adam Schiavone, manba

Men buni birinchi marta yozgan odamga pul to'lashni xohlayman, lekin men bu linkni yo'qotib qo'ygandim.

Shaxsiy yordamchi to'plamimdan:

    /// 
/// Generates a Relative Path for targetPath, from basePath ///
 
    /// 
The Directory to start from.
    /// 
Full Path to file or folder that is to be made relative
    /// The relative location of targetPath, from basePath
    /// Tested in Util.Tests.IO.PathUtilsTests
    public static string MakeRelativePath(string basePath, string targetPath)
    {
        if (basePath == targetPath)
            return "";

        if (!basePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            basePath += Path.DirectorySeparatorChar;

        if (String.IsNullOrEmpty(basePath)) throw new ArgumentNullException("basePath");
        if (String.IsNullOrEmpty(targetPath)) throw new ArgumentNullException("targetPath");

        var fromUri = new Uri(basePath);
        var toUri = new Uri(targetPath);

        if (fromUri.Scheme != toUri.Scheme) { return targetPath; }//path can't be made relative.

        var relativeUri = fromUri.MakeRelativeUri(toUri);
        var relativePath = Uri.UnescapeDataString(relativeUri.ToString());

        if (toUri.Scheme.Equals("file", StringComparison.InvariantCultureIgnoreCase))
        {
            relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
        }

        return relativePath;
    }

Foydalanish:

var output = @"C:\Users\Documents\MyWeb\Slide\Main\slider2.png";
var basePath = @"C:\Users\Documents\MyWeb\";

var rel = MakeRelativePath(basePath, output);
0
qo'shib qo'ydi
Bu shaxs bormi?
qo'shib qo'ydi muallif Sinatr, manba
Ha! Buni birinchi marta olganimga o'xshaydi.
qo'shib qo'ydi muallif Adam Schiavone, manba

Siz shunday bir narsani ishlatishingiz mumkin:

string imageFile = @"C:\Users\Documents\MyWeb\Slide\Main\slider2.png";
string match = System.Text.RegularExpressions.Regex.Match(imageFile,@"\\Slide\\Main\\.*").Value;
0
qo'shib qo'ydi

Siz shunday bir narsani ishlatishingiz mumkin:

string imageFile = @"C:\Users\Documents\MyWeb\Slide\Main\slider2.png";
string match = System.Text.RegularExpressions.Regex.Match(imageFile,@"\\Slide\\Main\\.*").Value;
0
qo'shib qo'ydi

Siz buni osonlik bilan Uri bilan qilishingiz mumkin, men uni sinab ko'rdim, shuning uchun u yaxshi ishlaydi

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main()
        {
            Uri uriAddress1 = new Uri("C:\\Users\\Documents\\MyWeb\\Slide\\Main\\slider2.png");
            Console.WriteLine("The parts are {0}{1}{2}", uriAddress1.Segments[5], uriAddress1.Segments[6], uriAddress1.Segments[7]);
            Console.ReadKey();
        }
    }
}
0
qo'shib qo'ydi

Siz buni osonlik bilan Uri bilan qilishingiz mumkin, men uni sinab ko'rdim, shuning uchun u yaxshi ishlaydi

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main()
        {
            Uri uriAddress1 = new Uri("C:\\Users\\Documents\\MyWeb\\Slide\\Main\\slider2.png");
            Console.WriteLine("The parts are {0}{1}{2}", uriAddress1.Segments[5], uriAddress1.Segments[6], uriAddress1.Segments[7]);
            Console.ReadKey();
        }
    }
}
0
qo'shib qo'ydi