我通过opendialog得到一个文件名(含路径),例如:c:\TestFile\111\222\3333\4444\5555\6666\myfile.txt,
由于路径太长我想得到类似这种c:\TestFile\111\222\...\6666\myfile.txt形式,怎么实现?有没有现成的函数可以使用。   我现在使用copy函数+文件名形式来获得,感觉不怎么理想。

解决方案 »

  1.   

    可以参考下如何获取相对路径
    http://apps.hi.baidu.com/share/detail/12708307
      

  2.   

    exe文件(*.exe)显示在文件类型里,中间用|隔开,|后面代表过滤exe以外的其他类型文件
    opendialog1.filter:='所有文件(*.*)|*.*';
    opendialog1.filter:='文本文件(*.txt)|*.txt';
    opendialog1.filter:='文本文件(*.txt)|*.txt|FLASH文件(*.swf)|*.swf';
    如果显示2个以上的类型,中间用;隔开,如:
    支持文件类型(*.swf;*.jpg)|*.swf;*.jpg 
      

  3.   

    if opendialog1.execute then
      .......= opendialog1.filename
      

  4.   

    用OpenDialog1.FileName   如:   
      procedure   TForm1.Button1Click(Sender:   TObject);   
      begin   
      if   OpenDialog1.Execute   then   
          application.MessageBox(pchar(OpenDialog1.FileName),'',mb_ok)   
      end; 1:opendialog1:=topendialog.create(application.mainform);
    opendialog1.filter:='图形文件(*.123)|*.123|所有文件(*.*)|*.*|';//'所有文件(*.*)|*.*|
    opendialog1.DefaultExt:='123';
    opendialog1.InitialDir:=ExtractFileDir(application.exename); 
      

  5.   

    我想得到类似这种c:\TestFile\111\222\...\6666\myfile.txt形式,楼主上储位的代码都不行。
      

  6.   

    就是判断一下文件路径的长度 超过一定数量
    copy左边几个+'...'+copy右边几个 组成新字符串了//函数功能:得到中间省略的字符串
    function GetEllipsisString(const S: string; Len: Integer = 10): string;
    begin
      Result := S;
      if Length(Result) < 20 then
        Exit;
      Result := Copy(S, 1, Len)+ '...'+ Copy(S, Length(S) + 1 - Len, Len);
    end;
      

  7.   

    有现成的函数:GetShortPathName
    但是在你所描述的路径里并不能起作用,能简化描述的路径都是有规则的
      

  8.   

    LZ下一个万一的《Delphi全掌握》吧,万一里面有说的,其实就是一个系统函数。
      

  9.   

    function PathCompactPathExW(pszOut, pszSrc: PWideChar; cchMax: UINT;
      dwFlags: DWORD): BOOL; stdcall; External 'shlwapi.dll' Name 'PathCompactPathExW';第一个是输出
    第二个是输入
    第三个是设定长度,第四个0
    procedure TForm1.Button1Click(Sender: TObject);
    var
      pstr: array [0 .. MAX_PATH] of WideChar;
    begin
      if OpenDialog1.Execute then
      begin
        PathCompactPathExW(pstr, PWideChar(OpenDialog1.FileName), 100, 0);
        Edit1.Text := String(pstr);
      end;
    end;
    原始路径是:C:\Windows\winsxs\msil_aspnet_compiler.resources_b03f5f7f11d50a3a_6.1.7600.16385_zh-cn_d255d2f0ed7db8b4\aspnet_compiler.resources.dll得出的结果是:C:\Windows\winsxs\msil_aspnet_compiler.resources_b03f5f7f11d50a3a_...\aspnet_compiler.resources.dll
      

  10.   


    没有人会这样来保存变量,这个函数的应用主要是给使用者带来清晰的文件名显示,比如一个label的宽度不足,用户也能清楚文件名是什么,而不是一个半截的路径,特别是在复制或者处理多个文件时。
      

  11.   

    老大,你的代码在delphi7下通不过啊,函数申明过了。
    错误信息如下:
    [Warning] Unit1.pas(37): Suspicious typecast of TCaption to PWideChar
    [Error] Unit1.pas(44): Declaration expected but end of file found
    [Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
      

  12.   

    他的代码是2009下写的吧,delphi7下用PathCompactPathEx吧
    或者声明一个widestring的变量