简体   繁体   English

如何从delphi 7 aplpication运行mklink系统命令(cmd.exe)

[英]How can I run a mklink system command (cmd.exe) from a delphi 7 aplpication

In my application at runtime I want to run the cmd line Mklink /d (link location) (actual file/folder location). 在运行时的应用程序中,我想运行cmd行Mklink / d(链接位置)(实际文件/文件夹位置)。

So far I have: 到目前为止,我有:

symLink := PChar('/d C:\Data\'+fileName+' N:\"Database Archives"\'+fileName);

ShellExecute(0, nil, 'Mklink', symLink, nil, SW_SHOWNORMAL);

but I have also tried: 但我也尝试过:

symLink := PChar('Mklink /d C:\Data\'+fileName+' N:\"Database Archives"\'+fileName);

ShellExecute(0, nil, 'cmd.exe', symLink, nil, SW_SHOWNORMAL);

Am I along the right lines? 我是沿着正确的路线吗? Is there any documentation to explain each parameter required by ShellExecute ? 是否有任何文档可以解释ShellExecute所需的每个参数?

Simply put, don't do this. 简单地说,不要这样做。 Use the API. 使用API​​。 It's called CreateSymbolicLink . 它叫做CreateSymbolicLink Pass the SYMBOLIC_LINK_FLAG_DIRECTORY flag. 传递SYMBOLIC_LINK_FLAG_DIRECTORY标志。

This is much more robust and allows you to report errors properly, cope with systems that don't have cmd.exe and so on. 这更加强大,允许您正确报告错误,处理没有cmd.exe系统等等。

Do note that creating symbolic links requires the process to be elevated. 请注意,创建符号链接需要提升进程。 I trust you already know that. 我相信你已经知道了。 Do also check for errors as described the linked documentation. 还要检查链接文档中描述的错误。

More broadly you'll need to reconsider the specific paths in your question. 更广泛地说,您需要重新考虑问题中的具体路径。 You cannot make cross volume symbolic links. 您无法制作跨卷符号链接。

As regards documentation, these are Windows functions. 至于文档,这些是Windows功能。 Just type the name into a search engine and you will find the MSDN docs. 只需在搜索引擎中输入名称,您就会找到MSDN文档。


In an older Delphi you'll need to define the function because the supplied header translations don't have it. 在较旧的Delphi中,您需要定义该函数,因为提供的头文件翻译没有它。

const
  SYMBOLIC_LINK_FLAG_DIRECTORY = $00000001;

function CreateSymbolicLinkA(
  lpSymlinkFileName: PAnsiChar;
  lpTargetFileName: PAnsiChar;
  dwFlags: DWORD
): Boolean; stdcall; external 'kernel32';

function CreateSymbolicLinkW(
  lpSymlinkFileName: PWideChar;
  lpTargetFileName: PWideChar;
  dwFlags: DWORD
): Boolean; stdcall; external 'kernel32';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM