2016-03-08 74 views
0

因爲我可以將所有.txt文件從一個文件夾上傳到FTP文件夾。我只能上傳一個文件,但我需要全部上傳一個文件夾內您的計算機上的FTP文件夾中的文件C#將PC上的文件夾中的所有文件上傳到FTP文件夾

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://123.456.789.00/folder1/folder2" + "/" + Path.GetFileName("D:\\folderUpload\\1test.txt")); 

      request.Method = WebRequestMethods.Ftp.UploadFile; 
      request.Credentials = new NetworkCredential("username", "pass"); 
      request.UsePassive = true; 
      request.UseBinary = true; 
      request.KeepAlive = false; 

      FileStream stream = File.OpenRead("D:\\folderUpload\\1test.txt"); 
      byte[] buffer = new byte[stream.Length]; 

      stream.Read(buffer, 0, buffer.Length); 
      stream.Close(); 

      Stream reqStream = request.GetRequestStream(); 
      reqStream.Write(buffer, 0, buffer.Length); 
      reqStream.Close(); 

      MessageBox.Show("Upload OK"); 
+2

重構代碼,並創建接受文件,並上傳到FTP服務器的方法。然後,對於源文件夾中的每個文件,調用該方法並傳遞文件。 –

+0

[使用c#將文件上傳到ftp]可能的副本(http://stackoverflow.com/questions/15268760/upload-file-to-ftp-using-c-sharp) – Brandon

回答

0

頭可以做這樣的事情(也可以,如果你想保持改善原樹或並行......):

static public void Main(string[] args) 
    { 
     DirectoryInfo directory = DirectoryInfo(@"C:\PathToUpload"); 
     foreach(var file in directory.GetFiles(*)){ 
      UploadFile(file, "ftp://123.456.789.00/folder1/folder2"); 
     } 

    MessageBox.Show("Upload OK"); 

} 


public void UploadFile(FileInfo file,string ftpUrl){  
    FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpUrl + "/" + file.Name); 
    request.Method = WebRequestMethods.Ftp.UploadFile; 
    request.Credentials = new NetworkCredential("username", "pass"); 
    request.UsePassive = true; 
    request.UseBinary = true; 
    request.KeepAlive = false; 
    FileStream stream = File.OpenRead("D:\\folderUpload\\1test.txt"); 
    byte[] buffer = new byte[stream.Length]; 
    stream.Read(buffer, 0, buffer.Length); 
    stream.Close(); 
    Stream reqStream = request.GetRequestStream(); 
    reqStream.Write(buffer, 0, buffer.Length); 
    reqStream.Close(); 
} 
-1
public static void uploadFolder(string source, string uploadpath) 
{ 
    WebRequest request = WebRequest.Create(uploadpath); 
    request.Credentials = new NetworkCredential(userName, password); 
    string[] files = Directory.GetFiles(source, "*.*"); 
    string[] subFolders = Directory.GetDirectories(source); 
    foreach(string file in files) 
    { 
     request = WebRequest.Create(file); 
     request.Method = WebRequestMethods.Ftp.UploadFile; 
    } 
    foreach(string subFolder in subFolders) 
    { 
     request = WebRequest.Create(uploadpath + "/"+ Path.GetFileName(subFolder)); 
     request.Method = WebRequestMethods.Ftp.MakeDirectory; <br/> 
     request.Credentials = new NetworkCredential(userName,password); 
     uploadFolder(subFolder, uploadpath+"/"+Path.GetFileName(subFolder)); 
    } 
} 
+0

我們通常會要求您提供一些上下文給您通過解釋它如何工作來回答。 https://stackoverflow.com/help/answering – TidyDev

+0

請注意,您不應該使用
標籤來格式化您的代碼。改爲使用「編輯」菜單來設置文本格式。 – Alex