文件路径简化显现

yjw88123 2011-01-20 01:03:13
就像word打开最近打开过的文件显现,当路径过长时候显示成C:\...\MyDocuments\word1.doc ,通过...来简化路径
...全文
142 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
rabbitlzx 2011-01-20
  • 打赏
  • 举报
回复
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();
}


C:\MyDocuments1\MyDocuments2\MyDocuments3\MyDocuments\word1.doc
C:\...\MyDocuments\word1.doc

C:\MyDocuments1\MyDocuments\word1.doc
C:\...\MyDocuments\word1.doc

C:\MyDocuments\word1.doc
C:\MyDocuments\word1.doc
inttlegence 2011-01-20
  • 打赏
  • 举报
回复
private void Test(PaintEventArgs e)
{
StringFormat format1 = new StringFormat();
string quote = @"C:\MyDocuments1\MyDocuments2\MyDocuments3\MyDocuments\word1.doc";
format1.Trimming = StringTrimming.EllipsisPath;
e.Graphics.DrawString(quote, this.Font, Brushes.Black,
new RectangleF(10.0F, 10.0F, 200.0F, 10.0F), format1);
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Test(e);
}
rabbitlzx 2011-01-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 yjw88123 的回复:]

保存到Tag我知道了,但是路径中的\...\中的内容如何省略的逻辑比较麻烦,不知道有没有更好的办法?
[/Quote]晕 自己写一个函数,输入参数是全路径,输出是省略后的,具体的逻辑自己写,估计不会超过10行
yjw88123 2011-01-20
  • 打赏
  • 举报
回复
保存到Tag我知道了,但是路径中的\...\中的内容如何省略的逻辑比较麻烦,不知道有没有更好的办法?
rabbitlzx 2011-01-20
  • 打赏
  • 举报
回复
在菜单项上显示 C:\...\MyDocuments\word1.doc,然后将完整路径保存到Tag属性中,需要的时候再取出来
wy811007 2011-01-20
  • 打赏
  • 举报
回复
2L正解. 5L 也是..
wuyq11 2011-01-20
  • 打赏
  • 举报
回复
Path.GetFileName
Path.GetDirectoryname实现数据省略
rczjp 2011-01-20
  • 打赏
  • 举报
回复
你显示可以按照那个咖啡猫的做,实际的路径你还是保存全部的嘛
yjw88123 2011-01-20
  • 打赏
  • 举报
回复
有更好更实用的方法吗,我现在需要做成Word打开最近打开文件那种菜单样式,显示成简约路径是可以,但我的详细路径还是有要用的。希望有更详细的代码
q107770540 2011-01-20
  • 打赏
  • 举报
回复

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
}
shangxin200093 2011-01-20
  • 打赏
  • 举报
回复
一般都是通过字符串长度来控制的,当超过某个长度就截取一个特定的范围进行替换,例如第一个“\”到第二个“\”之间的字符。或者可以用其他的条件来替换。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧