110,534
社区成员
发帖
与我相关
我的任务
分享
private const int LastFullDisplayLength = 25; // ...后面最多有多少个字符
private const int MaxDisplayLength = 30;
private static string SimplifyPath(string fullPath)
{
//Console.WriteLine(fullPath);
string retVal = fullPath;
if (fullPath.Length > MaxDisplayLength)
{
retVal = retVal.Substring(fullPath.Length - LastFullDisplayLength);
int firstPos = retVal.IndexOf('\\');
retVal = retVal.Substring(firstPos);
retVal = System.IO.Path.GetPathRoot(fullPath) + "..." + retVal;
}
//Console.WriteLine(retVal);
return retVal;
}
static void Main(string[] args)
{
string url = @"C:\MyDocuments1\MyDocuments2\MyDocuments3\MyDocuments\word1.doc";
SimplifyPath(url);
url = @"C:\MyDocuments1\MyDocuments\word1.doc";
SimplifyPath(url);
url = @"C:\MyDocuments\word1.doc";
SimplifyPath(url);
Console.ReadKey();
}
void Main()
{
string url=@"C:\MyDocuments1\MyDocuments2\MyDocuments3\MyDocuments\word1.doc";
string newURL=url;
if(url.Length >25)
{
newURL=url.Substring(0,3)+"..."+url.Substring(url.Length-22);
}
Console.WriteLine(newURL);
//C:\...\MyDocuments\word1.doc
}