如何使用默认文本编辑器打开文件?

Hid*_*den 2 delphi shellexecute configuration-files

我想打开一个*.conf文件.我想用标准Windows编辑器(例如,notepad.exe)打开此文件.

我目前有这个ShellExecute代码:

var
  sPath, conf: String;
begin
  try
  sPath := GetCurrentDir + '\conf\';
  conf := 'nginx.conf';
ShellExecute(Application.Handle, 'open', PChar(conf), '', Pchar(sPath+conf), SW_SHOW);
  except
    ShowMessage('Invalid config path.');
  end;
end; 
Run Code Online (Sandbox Code Playgroud)

但没有任何反应.那么我应该改变什么呢?

Dav*_*nan 7

如何使用默认文本编辑器打开文件?

您需要使用ShellExecuteEx并使用该lpClass成员SHELLEXECUTEINFO来指定您要将文件视为文本文件.像这样:

procedure OpenAsTextFile(const FileName: string);
var
  sei: TShellExecuteInfo;
begin
  ZeroMemory(@sei, SizeOf(sei));
  sei.cbSize := SizeOf(sei);
  sei.fMask := SEE_MASK_CLASSNAME;
  sei.lpFile := PChar(FileName);
  sei.lpClass := '.txt';
  sei.nShow := SW_SHOWNORMAL;
  ShellExecuteEx(@sei);
end;
Run Code Online (Sandbox Code Playgroud)

将完整路径传递给文件FileName.