views:

667

answers:

1

I want to get a file size over network via System.Net.FileWebRequest. For example: \IP\c$\sampleFile.txt

I supply the credentials for accessing file via passing it to FilewebRequest's credentials but the code below returns an access to path denied error. What is worng here? How can I access files over network? (I have crediantals to access file)

 System.Net.FileWebRequest request = (System.Net.FileWebRequest)
 System.Net.FileWebRequest.Create(url);
 request.PreAuthenticate = true;
 request.Credentials = new NetworkCredential(_clientUser,_clientPass);
 FileWebResponse response = (System.Net.FileWebResponse) request.GetResponse();

 // gets the size of the file in bytes    
 Int64 iSize = response.ContentLength;
 response.Close();
A: 

It looks like the file you are trying to get is on your network rather than through an http call which is what FileWebRequest is for.

Try:

FileInfo fileInfo = new FileInfo(filePath);
long fileBytes = fileInfo.Length;
long fileKBytes = fileInfo.Length / 1024;

The filepath being your \IP\c$\sampleFile.txt. Make sure it is accessible by your machine to read.

Francis Gilbert
The problem is here that.I have crediantials to access file (username and pass)If first I try to access file over run>command "\\IP\c$\sampleFile.txt" windows opens authenticate windows and than using code gives no error..but i have to succeed the authentication in code also...
dankyy1