unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellAPI;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure ConnectServer;
procedure DisConnectServer;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
g_bFlag:boolean;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ConnectServer;
end;
procedure TForm1.ConnectServer;
var
NetSource : TNetResource;
dwResult:DWORD;
begin
g_bFlag:=True;
with NetSource do
begin
dwType := RESOURCETYPE_ANY;
lpLocalName :='';
lpRemoteName:='\\192.18.11.21';
lpProvider :='';
end;
dwResult:=WnetAddConnection2(NetSource,'123456','lxx',CONNECT_UPDATE_PROFILE);
if dwResult<>0 then
begin
ShowMessage('连接服务器失败!');
g_bFlag:=False;
end
else
begin
if winexec('net use \\192.18.11.21\I$ 123456 /user:lxx',SW_HIDE)>31 then
Sleep (3000)
else
begin
application.MessageBox('登录失败.','提示信息',48);
exit;
end;
//从服务里拷贝文件到本地D盘
if copyfile(PChar('I:/wenjianjia/2.txt'),PChar('d:/1.txt'),false) then
application.MessageBox('文件复制成功.','提示信息',64)
else
application.MessageBox('文件复制失败.','提示信息',48);
//直接打开共享文件夹
ShellExecute(0, 'open', pchar('I:/wenjianjia/'), '', '', SW_SHOW);
ShowMessage('OK!');
end;
end;
procedure TForm1.DisConnectServer;
begin
if g_bFlag then
WNetCancelConnection2('\\192.18.11.21',CONNECT_UPDATE_PROFILE,True);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
DisConnectServer;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellAPI;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure ConnectServer;
procedure DisConnectServer;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
g_bFlag:boolean;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ConnectServer;
end;
procedure TForm1.ConnectServer;
var
NetSource : TNetResource;
dwResult:DWORD;
begin
g_bFlag:=True;
with NetSource do
begin
dwType := RESOURCETYPE_ANY;
lpLocalName :='';
lpRemoteName:='\\192.18.11.21';
lpProvider :='';
end;
dwResult:=WnetAddConnection2(NetSource,'123456','lxx',CONNECT_UPDATE_PROFILE);
if dwResult<>0 then
begin
ShowMessage('连接服务器失败!');
g_bFlag:=False;
end
else
begin
if winexec('net use \\192.18.11.21\I$ 123456 /user:lxx',SW_HIDE)>31 then
Sleep (3000)
else
begin
application.MessageBox('登录失败.','提示信息',48);
exit;
end;
//从服务里拷贝文件到本地D盘
if copyfile(PChar('I:/wenjianjia/2.txt'),PChar('d:/1.txt'),false) then
application.MessageBox('文件复制成功.','提示信息',64)
else
application.MessageBox('文件复制失败.','提示信息',48);
//直接打开共享文件夹
ShellExecute(0, 'open', pchar('I:/wenjianjia/'), '', '', SW_SHOW);
ShowMessage('OK!');
end;
end;
procedure TForm1.DisConnectServer;
begin
if g_bFlag then
WNetCancelConnection2('\\192.18.11.21',CONNECT_UPDATE_PROFILE,True);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
DisConnectServer;
end;
end.
这篇博客介绍了如何使用Delphi程序连接到局域网中的共享文件夹,通过WnetAddConnection2函数建立连接,并进行文件的复制操作。接着,展示了如何断开连接,以及使用ShellExecute打开共享文件夹。对于需要在Delphi中处理网络文件操作的开发者来说,这是一个实用的示例。

1584

被折叠的 条评论
为什么被折叠?



