RegSetFileType.pas 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. unit RegSetFileType;
  2. interface
  3. uses
  4. SysUtils, Windows;
  5. {
  6. Example usage:
  7. RegSetAssociations('.MYX', 'myCompany.MYX', 'myCompany`s X File',
  8. 'application/x-myCompany.MYX', ExtractFilePath(Application.ExeName) + '\Graphics\Icon1.ico');
  9. RegSetOpenWith('myCompany.MYX', Application.ExeName + ' %1');
  10. }
  11. procedure RegSetAssociation(inExtension, inFileType, inTypeDesc, inContentType, inIconPath: String);
  12. procedure RegSetOpenWith(inFileType, inOpenWith: String);
  13. procedure RegSetFileTypeAutoOpenInIE(inFileType: String; inAutoOpenFromIE: Boolean = true);
  14. procedure RegClearIEOpenKey(inFileExt: String);
  15. procedure RegClearAssociation(inExtension, inFileType: String);
  16. implementation
  17. uses
  18. Registry;
  19. const
  20. cExceptMsg = 'RegSetFileType Failure';
  21. procedure RegSetAssociation(inExtension, inFileType, inTypeDesc, inContentType, inIconPath: String);
  22. var
  23. Reg: TRegistry;
  24. begin
  25. Reg := TRegistry.Create;
  26. try
  27. Reg.RootKey := HKEY_CLASSES_ROOT;
  28. if not Reg.OpenKey('\'+inExtension, True) then
  29. raise Exception.Create(cExceptMsg);
  30. Reg.WriteString('', inFileType);
  31. Reg.WriteString('Content Type', inContentType);
  32. if not Reg.OpenKey('\'+inFileType, True) then
  33. raise Exception.Create(cExceptMsg);
  34. Reg.WriteString('', inTypeDesc);
  35. if not Reg.OpenKey('DefaultIcon', True) then
  36. raise Exception.Create(cExceptMsg);
  37. Reg.WriteString('', inIconPath);
  38. Reg.CloseKey;
  39. finally
  40. Reg.CloseKey;
  41. Reg.Free;
  42. end;
  43. end;
  44. procedure RegSetOpenWith(inFileType, inOpenWith: String);
  45. var
  46. Reg: TRegistry;
  47. begin
  48. Reg := TRegistry.Create;
  49. try
  50. Reg.RootKey := HKEY_CLASSES_ROOT;
  51. if not Reg.OpenKey('\'+inFileType, false) then
  52. raise Exception.Create(cExceptMsg);
  53. if not Reg.OpenKey('shell', true) then
  54. raise Exception.Create(cExceptMsg);
  55. if not Reg.OpenKey('open', true) then
  56. raise Exception.Create(cExceptMsg);
  57. if not Reg.OpenKey('command', true) then
  58. raise Exception.Create(cExceptMsg);
  59. Reg.WriteString('', inOpenWith);
  60. finally
  61. Reg.CloseKey;
  62. Reg.Free;
  63. end;
  64. end;
  65. procedure RegClearIEOpenKey(inFileExt: String);
  66. var
  67. Reg: TRegistry;
  68. begin
  69. Reg := TRegistry.Create;
  70. try
  71. Reg.RootKey := HKEY_CURRENT_USER;
  72. Reg.DeleteKey('Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\' + inFileExt);
  73. finally
  74. Reg.CloseKey;
  75. Reg.Free;
  76. end;
  77. end;
  78. procedure RegClearAssociation(inExtension, inFileType: String);
  79. var
  80. Reg: TRegistry;
  81. begin
  82. Reg := TRegistry.Create;
  83. try
  84. Reg.RootKey := HKEY_CLASSES_ROOT;
  85. if Reg.OpenKey('\' + inExtension, false) then
  86. begin
  87. Reg.DeleteKey('\' + inExtension);
  88. end;
  89. if Reg.OpenKey('\' + inFileType, false) then
  90. begin
  91. Reg.DeleteKey('\' + inFileType);
  92. end;
  93. finally
  94. Reg.CloseKey;
  95. Reg.Free;
  96. end;
  97. end;
  98. procedure RegSetFileTypeAutoOpenInIE(inFileType: String; inAutoOpenFromIE: Boolean);
  99. var
  100. Reg: TRegistry;
  101. pikAutoOpenFlag: Integer;
  102. begin
  103. pikAutoOpenFlag := $00010000;
  104. Reg := TRegistry.Create;
  105. try
  106. Reg.RootKey := HKEY_CLASSES_ROOT;
  107. if not Reg.OpenKey('\'+inFileType, true) then
  108. raise Exception.Create(cExceptMsg);
  109. if inAutoOpenFromIE then
  110. Reg.WriteBinaryData('EditFlags', pikAutoOpenFlag, 4);
  111. finally
  112. Reg.CloseKey;
  113. Reg.Free;
  114. end;
  115. end;
  116. end.