Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 如何使用默认文本编辑器打开文件?_Delphi_Configuration Files_Shellexecute - Fatal编程技术网

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

Delphi 如何使用默认文本编辑器打开文件?,delphi,configuration-files,shellexecute,Delphi,Configuration Files,Shellexecute,我想打开一个*.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),

我想打开一个*.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; 
但什么也没发生。那么我应该改变什么呢

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

您需要使用和使用的
lpClass
成员来指定要将文件视为文本文件。像这样:

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;

将文件的完整路径作为
FileName

传递。主要问题是使用
nginx.conf
作为文件名。您需要完全限定的文件名(带驱动器和目录)。如果文件与EXE位于同一目录中,则应执行以下操作:

ShellExecute(Handle, nil,
  PChar(ExtractFilePath(Application.ExeName) + 'nginx.conf'),
  nil, nil, SW_SHOWNORMAL)
无需设置目录,通常应使用
SW\u SHOWNORMAL

此外,只有在运行应用程序的系统为
.conf
文件正确设置了文件关联的情况下,这才有效。如果运行应用程序的系统使用MS Paint打开
.conf
文件,则上面的行将启动MS Paint。如果根本没有关联,这条线就不会工作

您可以手动指定使用
notepad.exe

ShellExecute(Handle, nil, PChar('notepad.exe'),
  PChar(ExtractFilePath(Application.ExeName) + 'nginx.conf'),
  nil, SW_SHOWNORMAL)
现在我们开始
notepad.exe
,并将文件名作为第一个参数传递

第三,你不应该使用
try。
ShellExecute
可能由于“无效配置路径”以外的其他原因而失败,并且在任何情况下,它都不会引发异常。相反,考虑

if FileExists(...) then
  ShellExecute(...)
else
  MessageBox(Handle, 'Invalid path to configuration file', 'Error', MB_ICONERROR)
现在,回到主要问题。我的第一个代码片段仅在运行您的应用程序的系统碰巧有一个适合
.conf
文件的文件关联post时才起作用,而第二个代码片段将始终打开记事本。更好的选择可能是使用用于打开
.txt
文件的应用程序。大卫的回答给出了一个例子。

使用

ShellExecute(0, 'Open', PChar(AFile), nil, '', 1{SW_SHOWNORMAL});
在Delphi DX10中,此函数定义在

Winapi.ShellAPI


+1我没有发现文件名错误,并假设关联是问题所在。我将留下我的答案,因为它显示了如何确保将.conf文件视为文本文件。ShellExecute是一个Windows API调用,在任何情况下都不会引发Delphi异常。错误由返回值指示,
ShellExecute
的错误处理实际上是毫无希望的。如果要进行真正的错误检查,需要在与
.conf
没有关联的计算机上使用
ShellExecuteEx
@polymopin(例如,我现在所在的计算机),这里的第一个代码示例将无法工作。第二个示例强制用户使用记事本,我不喜欢这样。我从未见过记事本,因为它不是我默认的文本编辑器。您肯定想做的是使用用户首选的文本文件编辑器打开文本文件。我可能会将类名设置为
.txt
,因为
txtfile
不能保证是所有系统上文本文件的注册progid。有些应用确实改变了它。@RemyLebeau谢谢你。我承认,我对shell的类名知之甚少,这取决于您的经验和知识。实际上,我以前从未需要在
ShellExecuteEx()
中使用类名。我只是对文档中所说的内容(类名可以是文件扩展名或progid)以及我对文件扩展名progid的一般工作原理的了解进行了评论。