2012-09-06 258 views
0

我試圖實現一個Windows服務應用程序,它從FTP服務器下載文件並將它們保存到數據庫中指定的網絡驅動器位置。 網絡共享文件夾授予完全訪問「所有人」從FTP服務器下載文件到網絡驅動器

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); 
} 

所以,在這裏我想下載FTP服務器上文件到網絡驅動器。但我得到以下錯誤:

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() 

我在這裏失去了一些東西?如何將文件從FTP服務器複製到網絡共享文件夾中?

+2

該服務在哪個帳戶下運行? –

+0

你是否檢查過每個人都可以訪問文件夾共享和目錄(NTFS權限)? – Robert

+0

是的,我忘了更改服務進程安裝程序的帳戶類型..謝謝大家 – devphil

回答

0

通常,您的服務將在僅位於本地主機上的帳戶下運行。因此,它不是從您的網絡上經過身份驗證的帳戶運行的。只需更改該服務正在運行的帳戶,以確保它是來自網絡AD的帳戶,因此它將成爲您所指的所有人組的一部分。

+0

Got it!謝謝! – devphil

相關問題