I’m trying to implement a windows service application that downloads files from the FTP server and save them into a network drive location specified in DB.
The network shared folder grants full access to “Everyone”
bool status = true;
string webError = string.Empty;
FtpWebResponse response = null;
Stream ftpStream = null;
FileStream outputStream = null;
FtpDownloadToFolder = @"\\servername\SharedFolder\";
fileName = "test.jpg";
FtpWebRequest reqFTP = WebRequest.Create(new Uri(FtpAddress + "/" + fileName)) as FtpWebRequest;
reqFTP.Credentials = new NetworkCredential(FtpUser, FtpPassword);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Timeout = FtpTimeout;
response = reqFTP.GetResponse() as FtpWebResponse;
ftpStream = response.GetResponseStream();
if (!FtpDownloadToFolder.EndsWith("\\"))
{
FtpDownloadToFolder = FtpDownloadToFolder + "\\";
}
outputStream = new FileStream(FtpDownloadToFolder + "/" + fileName, FileMode.Create);
long cl = response.ContentLength;
int bufferSize = 2048;
byte[] buffer = new byte[bufferSize];
int readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
so, here I’m trying to download a file from the FTP server into a network drive. But I get following error:
Access to the path '\\servername\SharedFolder\test.jpg' is denied.
System.Net.WebException: The remote server returned an error: 150 0.066 seconds (measured here), 3.78 Mbytes per second
---> System.IO.IOException: The operation is not allowed on non-connected sockets.
at System.Net.Sockets.NetworkStream.InitNetworkStream(Socket socket, FileAccess Access)
at System.Net.Sockets.NetworkStream..ctor(Socket socket, Boolean ownsSocket)
at System.Net.FtpControlStream.QueueOrCreateFtpDataStream(Stream& stream)
at System.Net.FtpControlStream.PipelineCallback(PipelineEntry entry, ResponseDescription response, Boolean timeout, Stream& stream)
at System.Net.CommandStream.PostReadCommandProcessing(Stream& stream)
at System.Net.CommandStream.PostSendCommandProcessing(Stream& stream)
at System.Net.CommandStream.ContinueCommandPipeline()
at System.Net.CommandStream.CheckContinuePipeline()
--- End of inner exception stack trace ---
at System.Net.FtpWebRequest.CheckError()
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.CommandStream.Abort(Exception e)
at System.Net.CommandStream.CheckContinuePipeline()
at System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
at System.Net.FtpDataStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.Net.FtpWebResponse.Close()
Am I missing something here? how do I copy a file from the FTP server into a network shared folder?
Typically your service is going to run under an account that is on the local host machine only. Thus it is not running from an authenticated account on your network. Just change the account the service is running under to make sure it is an account from your network AD and thus it will be part of the Everyone group you are referring to.