Read file on remote server in hidden share

  • Thread starter Thread starter Glen Conway
  • Start date Start date
G

Glen Conway

Hi,

I am trying to get the contents of a file in a hidden share on a remote
server, something like '\\server.domain.com\c$\program
files\application\document.xml'. When I try any of the standard
streamreader, textreader or file classes I get an access denied error
message (which I expect). If I go Start | Run and type this in, I get
prompted for network credentials. After supplying the correct credentials I
can access the file both through explorer and through the VB.NET application
(as expected) as my workstation is caching the credentials for me. I want
to be able to do this programmatically.

First I tried the networkcredential object, creating an xmlResolver and
using this on an xmlDocument to load the file. This didn't work as I
understand it's only for HTTP requests and the path is obviously UNC. I
then tried the LogonUser API which according to MSDN cannot authenticate for
remote access. LOGON32_LOGON_INTERACTIVE always returned an unknown
username or password error although LOGON32_LOGON_NETWORK returned ok but
doesn't return the right type of token. The sample on MSDN for
WindowsIdentity.Impersonate will not work on Windows 2000 which is no good
to me :(

I also tried using WMI to copy the file to the local computer but had
problems getting the Copy method of CIM_DataFile to work. I would rather
leave the file on the remote server than drag it across a possibly slow WAN
link. My next attempt was to use WNetUseConnection to map a network drive
dynamically but this has problems under XP and 2003 and looked like a really
messy solution.

If anyone can provide some code to accomplish this I would be eternally
grateful!

Thanks

Glen
 
If you want to read the file while using different credentials, try this:
System.Threading.Thread.CurrentPrincipal = CType(New
System.Net.NetworkCredential("user","pw"),
System.Security.Principal.IPrincipal)

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
Thanks Jan but I get a "Specified cast is not valid" exception. The method
that needs to access the file runs under the thread pool so I put the code
into the class the thread pool calls although I don't think that has
anything to do with the exception.

Cheers

Glen
 
Back
Top