標籤:use util absolute max normal begin 驅動 fill function
unit xPath; interface uses ShlwApi, Windows, SysUtils; /// <summary> /// 取絕對路徑的函數。需要引用 ShlwApi.pas /// </summary> /// <param name="BasePath">參考路徑</param> /// <param name="RelativePath">相對路徑</param> /// <code> /// S := GetAbsolutePath(‘C:\Windows\System32‘, ‘..\DEMO.TXT‘) // S 將得到 ‘C:\Windows\DEMO.TXT /// </code> function GetAbsolutePathEx(BasePath, RelativePath:string):string; /// <summary> /// 取得本身程式的路徑 /// </summary> function AppPath : string; function GetRelativePath(const Path, AFile: string): string; implementation function GetAbsolutePathEx(BasePath, RelativePath:string):string; var Dest:array [0..MAX_PATH] of char; begin FillChar(Dest,MAX_PATH+1,0); PathCombine(Dest,PChar(BasePath), PChar(RelativePath)); Result:=string(Dest); end; function GetRelativePath(const Path, AFile: string): string; function GetAttr(IsDir: Boolean): DWORD; begin if IsDir then Result := FILE_ATTRIBUTE_DIRECTORY else Result := FILE_ATTRIBUTE_NORMAL; end; var p: array[0..MAX_PATH] of Char; begin PathRelativePathTo(p, PChar(Path), GetAttr(False), PChar(AFile), GetAttr(True)); Result := StrPas(p); end; function AppPath : string; begin Result := extractFilePath(Paramstr(0)); end; end. {ExpandFileName() 返迴文件的全路徑(含磁碟機、路徑) ExtractFileExt() 從檔案名稱中抽取副檔名 ExtractFileName() 從檔案名稱中抽取不含路徑的檔案名稱 ExtractFilePath() 從檔案名稱中抽取路徑名 ExtractFileDir() 從檔案名稱中抽取目錄名 ExtractFileDrive() 從檔案名稱中抽取磁碟機名 ChangeFileExt() 改變檔案的副檔名 ExpandUNCFileName() 返回含有網路磁碟機的檔案全路徑 ExtractRelativePath() 從檔案名稱中抽取相對路徑資訊 ExtractShortPathName() 把檔案名稱轉化為DOS的8·3格式 MatchesMask() 檢查檔案是否與指定的檔案名稱格式匹配 ExtractFilePath(FileName:String) 該函數返迴路徑名,其結尾字元總是“\” ExtractFileDir(FileName:String) }
【轉】相對路徑和絕對路徑的轉換