views:

1451

answers:

4

Hi,

my names's Carlos Im from Brazil. Im trying to open a file like this:

image1.picture.loadfromfile('\\ntmemo01\c$\ozzy2.bmp');

but it doesnt work. Im receving the exception

class EFOpenError with message "Cannot open file '\ntmemo01\c$\ozzy2.bmp' Access denied."

Thanks, Carlos

+15  A: 

You have to use double backslashes.

image1.picture.loadfromfile('\\ntmemo01\c$\ozzy2.bmp');

If you still get the exception, then the file is inaccessible from your application.

The first thing you should do is making sure, that you can access the file using the Windows Explorer.

Just type it into the Run dialog of the start menu (WinKey+R) and see what happens. If it doesn't work, make it work there first and then go back to your program.

DR
image1.picture.loadfromfile('\\ntmemo01\c$\ozzy2.bmp') doesnt work too. Its returning the same exception
Added more to my answer.
DR
In Delphi I don't think you can use double backslashes \\ can you?
Chapel
Yes, it's possible. All file operations eventually come down to Win32 API calls and in this case to CreateFile. And CreateFile works with UNC paths. Also most of the VCL functions work properly, even ExtractFileDrive works. (It extracts the server name and the share name)
DR
+4  A: 

when you copy this exact same string in windows explorer, is the file opened? Otherwise it might be a rights problem, as suggested by the error.

birger
+6  A: 

Is the C: drive on ntmemo01 shared? If it's not shared, you can't access it. If it's shared but requires a user name and password to access, you'll have to access it differently. You can map a drive letter to it, providing a user name and password in the process:

const
  RemoteName = '\\ntmemo01\C$';
  UserName = 'yourusername';
  Password = 'yourpassword';

function MapNetworkDrive: Boolean;
var
  NetRes: TNetResource;
  Res: DWord;
begin
  Result := True;
  FillChar(NetRes, SizeOf(TNetResource), 0);
  NetRes.dwType := RESOURCETYPE_DISK;
  NetRes.lpRemoteName := PChar(RemoteName);
  NetRes.lpLocalName := 'H:';   // Whatever drive letter you want
  Res := WNetAddConnection2(NetRes, PChar(Password), PChar(UserName), 0);
  Result := (Res = NO_ERROR);
end;

To unmap afterwards:

function UnMapNetworkDrive: Boolean;
var
  Res: DWord;
begin
  Res := WNetCancelConnection2(PChar('H:'), 0, True); // same drive letter as above
  Result := (Res + NO_ERROR);
end;
Ken White
C$ is hidden administrative share, but mapping a drive should help. http://support.microsoft.com/kb/314984
stukelly
Yeah, I know. I have a disaster recovery site we update files on every half hour; I map to C$ on a machine 20 miles away using an IP address with the code above. As long as there's a user name and password set, it seems to work just fine.
Ken White
+4  A: 

As mentioned by DR, the filename requires double backslashes for a UNC path

The access denied message suggests the you do not have permission to access the C$ share on the ntmemo01 computer.

C$ a is hidden administrative share are you sure the current user account has the correct permissions? You make have to the map a drive first, as suggested by Ken White

Administrive shares are disabled by default in Windows Vista and Windows 7, unless you join a domain. You can be enable them manually as follows.

Click on the start button and in the search box type ‘regedit’ and hit enter.

Browse to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System.

Add a new DWORD called LocalAccountTokenFilterPolicy and give it a value of 1.

Reboot and yer done!

Source: http://www.paulspoerry.com/2007/05/09/how-to-access-administrative-shares-on-vista-c/

stukelly