在delphi中如何访问共享目录???
我已经在Linux建立了和windows一样的共享目录和文件夹,需要用户名和密码才能访问,我是想让程序启动时就建立连接,我的程序是用来获取该共享目录中的文件和写入文件,但我试过了,要在windows的开始菜单的运行方式下登陆该目录后,程序才可以正常运行,否则报找不到该文件目录的错误,请帮帮忙,看看可以用什么方法可以解决,谢谢!!
我的源码是:
procedure Tmonitor_course_form.Timer1Timer(Sender: TObject);
var
READ_Dir : string;
DstList: TStringList;
strList: TStringList;
i,j:Integer;
Item: TListItem;
MyErrorString: String;
begin
lvService.Clear;
MSGwuhanIni:=TIniFile.Create(GetCurrentDir+'\MyFile.ini');
READ_Dir:=MSGwuhanIni.ReadString('ProcessID', 'READ_Dir', '');
DstList:=TStringList.Create;
strList:=TStringList.Create;
try
if not FileExists(READ_Dir+'MyFile.TXT') then exit;
DstList.LoadFromFile(READ_Dir+'MyFile.TXT');
for i:=0 to DstList.Count-1 do
begin
strList.Clear;
ExtractStrings(['|'],[],PChar(DstList[i]),strList); if strList.Count=0 then Continue;
Item:=lvService.Items.Add;
Item.Caption:=strList[0];
for j:=1 to strList.Count-1 do
Item.SubItems.Add(strList[j]);
end;
finally
strList.Free;
DstList.Free;
end;
end;其中READ_Dir的值是:"\\192.16.10.22\postgre\"
用户名是:user
密码是:123
请各位帮帮忙,谢谢!!
我已经在Linux建立了和windows一样的共享目录和文件夹,需要用户名和密码才能访问,我是想让程序启动时就建立连接,我的程序是用来获取该共享目录中的文件和写入文件,但我试过了,要在windows的开始菜单的运行方式下登陆该目录后,程序才可以正常运行,否则报找不到该文件目录的错误,请帮帮忙,看看可以用什么方法可以解决,谢谢!!
我的源码是:
procedure Tmonitor_course_form.Timer1Timer(Sender: TObject);
var
READ_Dir : string;
DstList: TStringList;
strList: TStringList;
i,j:Integer;
Item: TListItem;
MyErrorString: String;
begin
lvService.Clear;
MSGwuhanIni:=TIniFile.Create(GetCurrentDir+'\MyFile.ini');
READ_Dir:=MSGwuhanIni.ReadString('ProcessID', 'READ_Dir', '');
DstList:=TStringList.Create;
strList:=TStringList.Create;
try
if not FileExists(READ_Dir+'MyFile.TXT') then exit;
DstList.LoadFromFile(READ_Dir+'MyFile.TXT');
for i:=0 to DstList.Count-1 do
begin
strList.Clear;
ExtractStrings(['|'],[],PChar(DstList[i]),strList); if strList.Count=0 then Continue;
Item:=lvService.Items.Add;
Item.Caption:=strList[0];
for j:=1 to strList.Count-1 do
Item.SubItems.Add(strList[j]);
end;
finally
strList.Free;
DstList.Free;
end;
end;其中READ_Dir的值是:"\\192.16.10.22\postgre\"
用户名是:user
密码是:123
请各位帮帮忙,谢谢!!
解决方案 »
- 用Action:= caFree释放窗体,总是出错
- 如何在数据库SQL里保存图片的路径以及如何将该数据库的图片显示?
- 首次安装delphi7使用dcomconnect+tclientdataset报“error loading midas.dll”
- Group header
- 请教一个关于类方法重载的设计问题,面向对象有经验请进
- 我想在StringGird的每个单元中与表table连接。不知怎么做,教教小弟吧。
- 关于豪杰的播放器的控制问题!!
- 请问怎样返回当前EXE文件的路径!!!
- 如何调用那个可以设置打印范围(从x页--x页)的那个对话框,如何使用它
- 关于焦点问题
- 关于WebSoap方式,数据库图象清除问题
- 关于mdi窗体
[/USER:[domainname\]username]
[/USER:[dotted domain name\]username]
[/USER:[username@dotted domain name]
[[/DELETE] | [/PERSISTENT:{YES | NO}]]NET USE {devicename | *} [password | *] /HOMENET USE [/PERSISTENT:{YES | NO}]
上面可以用指定的用户和密码连接共享目录,它肯定是调用了windows的某个API函数(即用指定的用户和密码连接共享目录),应该有一个API(或者几个API联用)可以实现这个功能
// Make a connection to the network resource.
dwResult = WNetAddConnection3 (
hwnd, // Handle to the owner window.
&nr, // Retrieved from enumeration.
NULL, // No password.
NULL, // Logged-in user.
CONNECT_UPDATE_PROFILE); // Update profile with
// connection data.
if (dwResult == ERROR_ALREADY_ASSIGNED)
{
MessageBox (hwnd, TEXT("Already connected to specified resource."),
TEXT("Info"), MB_OK);
return FALSE;
}else if (dwResult == ERROR_DEVICE_ALREADY_REMEMBERED)
{
MessageBox (hwnd, TEXT("Attempted reassignment of remembered device"),
TEXT("Info"), MB_OK);
return FALSE;
}
else if (dwResult != ERROR_SUCCESS)
{
ErrorHandler (hwnd, dwResult, TEXT("WNetAddConnection3"));
return FALSE;
}MessageBox (hwnd, TEXT("Connected to specified resource."),
TEXT("Info"), MB_OK);
此方法使用了windows单元中的函数“WNetAddConnection2”,此函数是用来与网络资源建立永久连接,当你建立连接后就可以操纵该网络资源,比如共享目录中的文件读和写等操作。。
var
NR: NETRESOURCE;
Ret: DWORD;
S: string;
begin
S := '\\remotemachine';
NR.dwType := RESOURCETYPE_ANY;
NR.lpLocalName := nil;
NR.lpRemoteName := PChar(S);
NR.lpProvider := nil;
Ret := WNetAddConnection2(NR,PChar(edtPwd.Text),PChar(edtUser.Text),CONNECT_UPDATE_PROFILE);
if Ret <> NO_ERROR then
begin
if Ret <> ERROR_EXTENDED_ERROR then RaiseLastWin32Error
else CallNetExtError;
end
else begin
Application.MessageBox('Login OK! ^_^','提示',MB_OK or MB_ICONINFORMATION);
end;
end;procedure CallNetExtError;
var
ErrorCode: Cardinal;
ErrBuf,NameBuf: string;
ShowMsg: string;
begin
SetLength(ErrBuf,MAX_PATH);
SetLength(NameBuf,MAX_PATH);
if WNetGetLastError(ErrorCode,PChar(ErrBuf),MAX_PATH+1,PChar(NameBuf),MAX_PATH+1) = NO_ERROR then
begin
ShowMsg := 'Error Code:' + IntToStr(ErrorCode) + #13#10;
ShowMsg := ShowMsg + 'Error String:' + ErrBuf + #13#10;
ShowMsg := ShowMsg + 'Error Provider:' + NameBuf;
ShowMessage(ShowMsg);
end
else ShowMessage('Invalid Buffer Size! :-(');
end;