在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
请各位帮帮忙,谢谢!!

解决方案 »

  1.   

    NET USE [devicename | *] [\\computername\sharename[\volume] [password | *]]
            [/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联用)可以实现这个功能
      

  2.   

    ado里面有个stream对象,其中可以用loadfromfile读取文件内容,不过是否可以自由的创建和读取文件,可能与浏览器的安全和操作系统的安全设置有关
      

  3.   

    MSDN上的一段代码
    // 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);
      

  4.   

    使用下面的方法可以解决你的问题
    此方法使用了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;