Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 将完整路径显示为省略号的较短路径_.net_String_Vb.net - Fatal编程技术网

.net 将完整路径显示为省略号的较短路径

.net 将完整路径显示为省略号的较短路径,.net,string,vb.net,.net,String,Vb.net,我需要在按钮中显示路径。 例如,此路径可能是 "c:\users\myuser\desktop\somefolder" 按钮的大小是固定的,因此我正在寻找一种将文本转换为省略号版本的方法 我记得我看到过一个函数,它可以将如此长的路径转换为 "c:\users\...\somefolder" 但我不记得这是一个定制的解决方案,还是在.NET中有一种内置的方法来实现这一点 我不是指将字符串转换为 "c:\users\myuser\deskto~" 谢谢。一个简单的实现可能如下所示: var p

我需要在按钮中显示路径。 例如,此路径可能是

"c:\users\myuser\desktop\somefolder"
按钮的大小是固定的,因此我正在寻找一种将文本转换为省略号版本的方法

我记得我看到过一个函数,它可以将如此长的路径转换为

"c:\users\...\somefolder"
但我不记得这是一个定制的解决方案,还是在.NET中有一种内置的方法来实现这一点

我不是指将字符串转换为

"c:\users\myuser\deskto~"

谢谢。

一个简单的实现可能如下所示:

var pathString = @"c:\users\myuser\desktop\somefolder";
var maxStringLength = 25;

while(pathString.Length > maxStringLength)
{
    var splitPath = pathString.Split('\\').ToList();
    splitPath.Remove("...");
    splitPath[splitPath.Count - 2] = "...";
    pathString = string.Join("\\", splitPath);
}

请记住,这是一个非常天真的观点,什么构成了一条道路。您可能会发现需要更改它,并根据需要编写适当的测试。

这是一个自定义解决方案。结果是一个仅用于显示的无效路径。