如果通过文件后缀名得到文件类型?(50分)

  • 主题发起人 主题发起人 lzf1010
  • 开始时间 开始时间
说的不太清楚,但文件后缀名不能真正反应出文件类型....
 
应该可以获取系统注册的类型信息的,打开windows文件夹不是可以显示出文件类型出来吗?
 
直接从注册表里面读取指定扩展名的信息。
 
讀寫注冊表..
 
uses<br> &nbsp;Sysutils, &nbsp;Graphics, ShellApi ;<br>Const<br> &nbsp;PathSeparator &nbsp; &nbsp;= '/';<br><br>//从注册表中读取文件类型<br>function GetFileType(const ExtName: string): string;<br>begin<br> &nbsp;Result := UpperCase( ExtName );<br> &nbsp;if Result &lt;&gt; '' then<br> &nbsp; &nbsp;Result := Copy( Result , 2 , Length(Result) - 1);<br><br> &nbsp;Exit;<br><br> &nbsp;if ExtName = '' then<br> &nbsp; &nbsp;Result := ''<br> &nbsp;else<br> &nbsp;begin<br> &nbsp; &nbsp;Result := RegReadStringDef(HKEY_Classes_Root, ExtName, 'Content Type', '');<br> &nbsp; &nbsp;if Result = '' then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if ExtName[1] = '.' then<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := Copy(ExtName, 2, Length(ExtName))<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := ExtName;<br><br> &nbsp; &nbsp; &nbsp;Result := Result + ' 文件';<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;Result := UpperCase(Result);<br> &nbsp;end;<br>end;<br><br>//------------------------------------------------------------------------------<br>//把FileList中文件的图标加到ImageList中去<br><br>function GetFilesImageToList(ImageList: TImageList; FileName: string;<br> &nbsp;IsSmallIcon: Boolean): Boolean;<br>var<br> &nbsp;F_icon: TIcon;<br> &nbsp;shinfo: TSHFILEINFO;<br> &nbsp;isCreateFile: Boolean;<br> &nbsp;Flag: Integer;<br>begin<br> &nbsp;F_icon := TIcon.Create;<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp;if FileName &lt;&gt; '' then<br> &nbsp; &nbsp; &nbsp;if fileName[1] = '&quot;' then<br> &nbsp; &nbsp; &nbsp; &nbsp;Delete(FileName, 1, 1);<br><br> &nbsp; &nbsp;if FileName &lt;&gt; '' then<br> &nbsp; &nbsp; &nbsp;if fileName[Length(fileName)] = '&quot;' then<br> &nbsp; &nbsp; &nbsp; &nbsp;FileName := Copy(FileName, 1, Length(fileName) - 1);<br><br> &nbsp; &nbsp;isCreateFile := false;<br> &nbsp; &nbsp;if not FileExists(fileName) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;fileName := GetWindowsTempFolder + '/' + ExtractFileName(fileName);<br> &nbsp; &nbsp; &nbsp;if not FileExists(fileName) then<br> &nbsp; &nbsp; &nbsp; &nbsp;isCreateFile := ZsFileCreate(fileName);<br> &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;if isSmallIcon then<br> &nbsp; &nbsp; &nbsp;Flag := (SHGFI_SMALLICON or SHGFI_ICON)<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;Flag := (SHGFI_LARGEICON or SHGFI_ICON);<br><br> &nbsp; &nbsp;SHGetFileInfo(Pchar(fileName), 0, shinfo, Sizeof(shinfo), Flag);<br><br> &nbsp; &nbsp;F_icon.Handle := shinfo.hIcon;<br> &nbsp; &nbsp;ImageList.AddIcon(F_icon); //把ICO装入ImageList中<br><br> &nbsp; &nbsp;if isCreateFile then<br> &nbsp; &nbsp; &nbsp;DeleteFile(pchar(fileName));<br> &nbsp;finally<br> &nbsp; &nbsp;F_Icon.Free;<br> &nbsp;end;<br> &nbsp;Result := True;<br>end;<br><br>function GetFilesImageToList(ImageList: TImageList; FileList: TStringList;<br> &nbsp;IsSmallIcon: Boolean): Boolean;<br>var i: integer;<br>begin<br> &nbsp;ImageList.Clear;<br> &nbsp;if IsSmallIcon then<br> &nbsp; &nbsp;ImageList.Height := 16<br> &nbsp;else<br> &nbsp; &nbsp;ImageList.Height := 32;<br> &nbsp;ImageList.Width := ImageList.Height;<br><br> &nbsp;for i := 0 to FileList.Count - 1 do<br> &nbsp; &nbsp;GetFilesImageToList(ImageList, FileList, IsSmallIcon);<br><br> &nbsp;result := True;<br>end;
 
function PathRemoveSeparator(const Path: string): string;<br>var<br> &nbsp;L: Integer;<br>begin<br> &nbsp;L := Length(Path);<br> &nbsp;if (L &lt;&gt; 0) and (AnsiLastChar(Path) = PathSeparator) then<br> &nbsp; &nbsp;Result := Copy(Path, 1, L - 1)<br> &nbsp;else<br> &nbsp; &nbsp;Result := Path;<br>end;<br><br>procedure StrResetLength(var S: AnsiString);<br>begin<br> &nbsp;SetLength(S, StrLen(PChar(S)));<br>end;<br><br>function GetWindowsTempFolder: string;<br>var<br> &nbsp;Required: Cardinal;<br>begin<br> &nbsp;Result := '';<br> &nbsp;Required := GetTempPath(0, nil);<br> &nbsp;if Required &lt;&gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;SetLength(Result, Required);<br> &nbsp; &nbsp;GetTempPath(Required, PChar(Result));<br> &nbsp; &nbsp;StrResetLength(Result);<br> &nbsp; &nbsp;Result := PathRemoveSeparator(Result);<br> &nbsp;end;<br>end;<br><br>function RelativeKey(const Key: string): PChar;<br>begin<br> &nbsp;Result := PChar(Key);<br> &nbsp;if (Key &lt;&gt; '') and (Key[1] = '/') then<br> &nbsp; &nbsp;Inc(Result);<br>end;<br><br> &nbsp;<br>function RegReadStringDef(const RootKey: HKEY; const Key, Name, Def: string): string;<br>var<br> &nbsp;RegKey: HKEY;<br> &nbsp;Size: DWORD;<br> &nbsp;StrVal: string;<br> &nbsp;RegKind: DWORD;<br>begin<br> &nbsp;Result := Def;<br> &nbsp;if RegOpenKeyEx(RootKey, RelativeKey(Key), 0, KEY_READ, RegKey) = ERROR_SUCCESS then<br> &nbsp;begin<br> &nbsp; &nbsp;RegKind := 0;<br> &nbsp; &nbsp;Size := 0;<br> &nbsp; &nbsp;if RegQueryValueEx(RegKey, PChar(Name), nil, @RegKind, nil, @Size) = ERROR_SUCCESS then<br> &nbsp; &nbsp; &nbsp;if RegKind in [REG_SZ, REG_EXPAND_SZ] then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;SetLength(StrVal, Size);<br> &nbsp; &nbsp; &nbsp; &nbsp;if RegQueryValueEx(RegKey, PChar(Name), nil, @RegKind, PByte(StrVal), @Size) = ERROR_SUCCESS then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetLength(StrVal, StrLen(PChar(StrVal)));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := StrVal;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;RegCloseKey(RegKey);<br> &nbsp;end;<br>end;
 
to jfyes:<br>你那个GetFileType函数有问题吧,是直接取后缀名而已
 
应该看文件头的前三个字节,用二进制文件编辑器查看。
 
读注册表
 
to 阿九:<br>这些信息保存在注册表的什么地方呢?
 
我个代码楼主吧<br>uses Registry<br><br>function TForm2.RegReadItem(ARoot: HKEY; APath, AKey: String): String;<br>var<br> &nbsp;sRegistry: TRegistry;<br>begin<br> &nbsp;Result := '';<br> &nbsp;sRegistry := TRegistry.Create;<br> &nbsp;try<br> &nbsp; &nbsp;with sRegistry do begin<br> &nbsp; &nbsp; &nbsp;RootKey := ARoot;<br> &nbsp; &nbsp; &nbsp;if OpenKey(APath,True) then begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if ValueExists(AKey) then Result := sRegistry.ReadString(AKey);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;sRegistry.CloseKey;<br> &nbsp; &nbsp;sRegistry.Free;<br> &nbsp;end;<br>end;<br><br>procedure TForm2.Button1Click(Sender: TObject);<br>var<br> &nbsp;sExt,sTemp: String;<br>begin<br> &nbsp;if not OpenDialog1.Execute then Exit;<br> &nbsp;sExt := ExtractFileExt(OpenDialog1.FileName);<br> &nbsp;if sExt = '' then Exit;<br> &nbsp;sTemp := RegReadItem(HKEY_CLASSES_ROOT,sExt,'');<br> &nbsp;sTemp := RegReadItem(HKEY_CLASSES_ROOT,sTemp,'');<br> &nbsp;ShowMessage(sTemp);<br>end;
 
多人接受答案了。
 
后退
顶部