Reference:http://www.dotblogs.com.tw/puma/archive/2008/03/16/1659.aspx
最近同事問了我如何利用asp.net寫有關ftp的存取.....
我去網路找了一些資料分享給大家呀....
記得using System.Net
c#範例...完整程式碼如下:
我去網路找了一些資料分享給大家呀....
記得using System.Net
c#範例...完整程式碼如下:
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Net;
12
13 public partial class ftp_CS : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 //建立目錄
18 FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://111.11.11.11:1234/C/puma/test2");
19 Request.Credentials = new NetworkCredential("id", "pwd");
20 Request.Method = WebRequestMethods.Ftp.MakeDirectory;
21 Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘
22 FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
23 Response.Close();
24
25 //修改檔案名稱
26 FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://111.11.11.11:1234/C/puma/ssa.txt");
27 Request.Credentials = new NetworkCredential("id", "pwd");
28 Request.Method = WebRequestMethods.Ftp.Rename;
29 Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘
30 Request.RenameTo = "NewName.txt";
31 FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
32 Response.Close();
33
34 //修改目錄名稱
35 FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://111.11.11.11:1234/C/puma/test");
36 Request.Credentials = new NetworkCredential("id", "pwd");
37 Request.Method = WebRequestMethods.Ftp.Rename;
38 Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘
39 Request.RenameTo = "test2";
40 FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
41 Response.Close();
42
43 //移除檔案
44 FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://111.11.11.11:1234/C/puma/NewName.txt");
45 Request.Credentials = new NetworkCredential("id", "pwd");
46 Request.Method = WebRequestMethods.Ftp.DeleteFile;
47 Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘
48 FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
49 Response.Close();
50
51 //移除目錄
52 FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://111.11.11.11:1234/C/puma/test2");
53 Request.Credentials = new NetworkCredential("id", "pwd");
54 Request.Method = WebRequestMethods.Ftp.RemoveDirectory;
55 Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘
56 FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
57 Response.Close();
58
59 }
60 }
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Net;
12
13 public partial class ftp_CS : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 //建立目錄
18 FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://111.11.11.11:1234/C/puma/test2");
19 Request.Credentials = new NetworkCredential("id", "pwd");
20 Request.Method = WebRequestMethods.Ftp.MakeDirectory;
21 Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘
22 FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
23 Response.Close();
24
25 //修改檔案名稱
26 FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://111.11.11.11:1234/C/puma/ssa.txt");
27 Request.Credentials = new NetworkCredential("id", "pwd");
28 Request.Method = WebRequestMethods.Ftp.Rename;
29 Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘
30 Request.RenameTo = "NewName.txt";
31 FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
32 Response.Close();
33
34 //修改目錄名稱
35 FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://111.11.11.11:1234/C/puma/test");
36 Request.Credentials = new NetworkCredential("id", "pwd");
37 Request.Method = WebRequestMethods.Ftp.Rename;
38 Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘
39 Request.RenameTo = "test2";
40 FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
41 Response.Close();
42
43 //移除檔案
44 FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://111.11.11.11:1234/C/puma/NewName.txt");
45 Request.Credentials = new NetworkCredential("id", "pwd");
46 Request.Method = WebRequestMethods.Ftp.DeleteFile;
47 Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘
48 FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
49 Response.Close();
50
51 //移除目錄
52 FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://111.11.11.11:1234/C/puma/test2");
53 Request.Credentials = new NetworkCredential("id", "pwd");
54 Request.Method = WebRequestMethods.Ftp.RemoveDirectory;
55 Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘
56 FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
57 Response.Close();
58
59 }
60 }
沒有留言:
張貼留言