如何通过程序来访问win2000服务器中共享的文件?(50分)

  • 主题发起人 主题发起人 chensh
  • 开始时间 开始时间
C

chensh

Unregistered / Unconfirmed
GUEST, unregistred user!
如何通过程序来访问win2000服务器中共享的文件(局域网中)?
因为需要登录所以很不好弄,
我用了一个映射驱动器的函数可以不成功,
请给我一些代码我急用。
另外从win98 winme winxp win2k 来访问win2k中共享文件都是一样的吗?
 
登录计算机的方法
WinExec(Pchar('net use //computername password /user:username',sw_Hide);
注销计算机的方法
WinExec(Pchar('net use //computername /delete',sw_Hide);

http://zhuwei.5235.cn 上有代码。
 
搜一下我回答的贴子吧
 
NETRESOURCE *lpNetResource=NULL;
lpNetResource=new NETRESOURCE;
lpNetResource->dwType=RESOURCETYPE_DISK;
lpNetResource->lpProvider=NULL;
lpNetResource->dwUsage=RESOURCEUSAGE_CONNECTABLE;
lpNetResource->lpLocalName="Z:";
lpNetResource->lpRemoteName="////tzserver//ftp";
WNetAddConnection2(lpNetResource,"",NULL,0);
delete lpNetResource;
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StrUtils, Grids;

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;

function ConnectLine(strDir,UserName,Password : string; var str: string) : Boolean;

var
Form1: TForm1;

implementation

{$R *.dfm}

function ConnectLine(strDir,UserName,Password : string; var str: string) : Boolean;
var
nRes: TNetResource;
begin
result := true;
if Midstr(strDir,2,2) = ':/' then
begin
exit;
end;
nRes.dwScope := RESOURCE_GLOBALNET;
nRes.dwType := RESOURCETYPE_ANY;
nRes.dwDisplayType := RESOURCEDISPLAYTYPE_SHARE;
nRes.dwUsage := RESOURCEUSAGE_CONNECTABLE;
nRes.lpLocalName := PChar('G:');;
nRes.lpRemoteName := PChar(strDir);
nRes.lpComment := nil;
nRes.lpProvider := nil;

if UserName = '' then UserName := 'Guest';
Result := false;
case WNetAddConnection2(nRes,Pchar(Password),Pchar(UserName), CONNECT_PROMPT) of
NO_ERROR: begin Result := true; str := 'Success'; end;
ERROR_ACCESS_DENIED: str :='Access to the network resource was denied.';
ERROR_ALREADY_ASSIGNED: str := 'The local device specified by lpLocalName is already connected to a network resource.';
ERROR_BAD_DEV_TYPE: str := 'The type of local device and the type of network resource do not match.';
ERROR_BAD_DEVICE: str := 'The value specified by lpLocalName is invalid.';
ERROR_BAD_NET_NAME: str := 'The value specified by lpRemoteName is not acceptable to any network resource provider. The resource name is invalid, or the named resource cannot be located.';
ERROR_BAD_PROFILE: str := 'The user profile is in an incorrect format.';
ERROR_BAD_PROVIDER: str := 'The value specified by lpProvider does not match any provider.';
ERROR_BUSY: str := 'The router or provider is busy, possibly initializing. The caller should retry.';
ERROR_CANCELLED: str := 'The attempt to make the connection was cancelled by the user through a dialog box from one of the network resource providers, or by a called resource.';
ERROR_CANNOT_OPEN_PROFILE: str := 'The system is unable to open the user profile to process persistent connections.';
ERROR_DEVICE_ALREADY_REMEMBERED: str := 'An entry for the device specified in lpLocalName is already in the user profile.';
ERROR_EXTENDED_ERROR: str := 'A network-specific error occured. Call the WNetGetLastError function to get a description of the error.';
ERROR_INVALID_PASSWORD: str := 'The specified password is invalid.';
ERROR_NO_NET_OR_BAD_PATH: str := 'A network component has not started, or the specified name could not be handled.';
ERROR_NO_NETWORK: str := 'There is no network present.';
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
strDir, userName, password: string;
str: string;
begin
strDir := '//192.168.0.30/d$';
userName := 'guest';
password := '333';
ConnectLine(strDir, userName, password, str);
Caption := str;

StringGrid1.Options:=[];
StringGrid1.DefaultDrawing:=false;
StringGrid1.ColWidths[0]:=18;
StringGrid1.Cells[1,1] := 'ABC';
StringGrid1.Cells[1,2] := 'DEF';
end;
end.

即炒即卖。不知道能值几分?
 
后退
顶部