如何在给定文件URL中仅显示Foldername和文件名? [英] How Do I Display Only Foldername And File Name In Given File Url?

查看:58
本文介绍了如何在给定文件URL中仅显示Foldername和文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



我有以下代码,我想显示文件名和即时文件夹名。





string filepath =test / abc / pqr / test1 / test2 / abxc.txt;





我想只显示/test2/abxc.txt;



文件夹和子文件夹名称和计数在运行时确定。



请建议。



谢谢。



I am having below code and I want to display the file name with immediate folder name.


string filepath="test/abc/pqr/test1/test2/abxc.txt";


and I want to display only "/test2/abxc.txt";

the folder and sub folder names and count is decided at run time.

Please suggest.

Thanks.

推荐答案

如果你总是希望字符串部分从倒数第二个'/'开始,那么这样做:

1. 拆分 [ ^ ]将完整的文件路径转换为数组通过'/'

2.然后简单地连接这个数组的最后两个元素。

参见示例:

If you always want the string part starting from the second last '/', then do this:
1. Split[^] the full filepath into an array by '/'
2. then simply concatenate the last two elements of this array.
see example:
using System;

public class Program
{
    public static void Main()
    {
        string filepath="test/abc/pqr/test1/test2/abxc.txt";

        // Split string on /.
        string[] parts = filepath.Split('/');
        int size = parts.Length;
        string result = "/" + parts[size - 2] + "/" + parts[size - 1];

        Console.WriteLine(result);

    }
}



给你:


which give you:

/test2/abxc.txt






直到现在我尝试下面的代码,这也有效for。



Hi,

Till now I tried below code and this is also working for.

string filepath="test/abc/pqr/test1/test2/abxc.txt";

var lastSlash= filepath .Substring(0, filepath .LastIndexOf("/"));

string secondLastSlash= filepath .Substring(lastSlash= .LastIndexOf("/") + 0);



< br $>




谢谢。





Thanks.


这篇关于如何在给定文件URL中仅显示Foldername和文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆