I am programming using Delphi 2010 on a 32-bit Windows 7 PC.
The following code works when run on my PC as admin.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellApi;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ShellExecute(0,'open','command','/c ipconfig /all > testipco.txt','',0);
showmessage('Test IP Config file should be written');
end;
end.
The file TESTIPCO.TXT is output and contains the same results as typing ipconfig /all into a Command Prompt window.
When I compile the file and run it on a 64-bit Windows 8.1 PC as admin, the program appears to run OK, displaying the message (with no errors) but the file TESTIPCO.TXT is not produced.
Even running the exe on a Windows 8 machine with compatibility set to run as Windows 7 doesn't work.
I just cannot work out why this is the case. I'm hoping that one of you bright people may be able to steer me in the right direction.
cmd.exe
.command
is from DOS and Win 9x. You would be better using CreateProcess to start a process. But in any case ShellExecute doesn't report errors properly. Not that you appear to check for errors. You state that the program doesn't show any errors but of course not - you did not check for errors. Anyway, when you decide to add error checking you'll want to use ShellExecuteEx for its better error reporting. Still, CreateProcess is the right function here. – David Heffernantestipco.txt
file isn't created? Perhaps it is created but in a different folder. Since you are not providing the absolute file location for output file the file should be created in current folder of the command prompt which is not necessarily the same as the current folder of your application. – SilverWarior