欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#操作ftp类分享

程序员文章站 2024-02-25 10:06:58
复制代码 代码如下:class ftp{    private string host = null;    p...

复制代码 代码如下:

class ftp
{
    private string host = null;
    private string user = null;
    private string pass = null;
    private ftpwebrequest ftprequest = null;
    private ftpwebresponse ftpresponse = null;
    private stream ftpstream = null;
    private int buffersize = 2048;

    public ftp(string hostip, string username, string password) { host = hostip; user = username; pass = password; }

    public void download(string remotefile, string localfile)
    {
        try
        {
            ftprequest = (ftpwebrequest)ftpwebrequest.create(host + "/" + remotefile);
            ftprequest.credentials = new networkcredential(user, pass);
            ftprequest.usebinary = true;
            ftprequest.usepassive = true;
            ftprequest.keepalive = true;
            ftprequest.method = webrequestmethods.ftp.downloadfile;
            ftpresponse = (ftpwebresponse)ftprequest.getresponse();
            ftpstream = ftpresponse.getresponsestream();
            filestream localfilestream = new filestream(localfile, filemode.create);
            byte[] bytebuffer = new byte[buffersize];
            int bytesread = ftpstream.read(bytebuffer, 0, buffersize);
            try
            {
                while (bytesread > 0)
                {
                    localfilestream.write(bytebuffer, 0, bytesread);
                    bytesread = ftpstream.read(bytebuffer, 0, buffersize);
                }
            }
            catch (exception ex) { console.writeline(ex.tostring()); }
            localfilestream.close();
            ftpstream.close();
            ftpresponse.close();
            ftprequest = null;
        }
        catch (exception ex) { console.writeline(ex.tostring()); }
        return;
    }

    public void upload(string remotefile, string localfile)
    {
        try
        {
            ftprequest = (ftpwebrequest)ftpwebrequest.create(host + "/" + remotefile);
            ftprequest.credentials = new networkcredential(user, pass);
            ftprequest.usebinary = true;
            ftprequest.usepassive = true;
            ftprequest.keepalive = true;
            ftprequest.method = webrequestmethods.ftp.uploadfile;
            ftpstream = ftprequest.getrequeststream();
            filestream localfilestream = new filestream(localfile, filemode.create);
            byte[] bytebuffer = new byte[buffersize];
            int bytessent = localfilestream.read(bytebuffer, 0, buffersize);
            try
            {
                while (bytessent != 0)
                {
                    ftpstream.write(bytebuffer, 0, bytessent);
                    bytessent = localfilestream.read(bytebuffer, 0, buffersize);
                }
            }
            catch (exception ex) { console.writeline(ex.tostring()); }
            localfilestream.close();
            ftpstream.close();
            ftprequest = null;
        }
        catch (exception ex) { console.writeline(ex.tostring()); }
        return;
    }

    public void delete(string deletefile)
    {
        try
        {
            ftprequest = (ftpwebrequest)webrequest.create(host + "/" + deletefile);
            ftprequest.credentials = new networkcredential(user, pass);
            ftprequest.usebinary = true;
            ftprequest.usepassive = true;
            ftprequest.keepalive = true;
            ftprequest.method = webrequestmethods.ftp.deletefile;
            ftpresponse = (ftpwebresponse)ftprequest.getresponse();
            ftpresponse.close();
            ftprequest = null;
        }
        catch (exception ex) { console.writeline(ex.tostring()); }
        return;
    }

    public void rename(string currentfilenameandpath, string newfilename)
    {
        try
        {
            ftprequest = (ftpwebrequest)webrequest.create(host + "/" + currentfilenameandpath);
            ftprequest.credentials = new networkcredential(user, pass);
            ftprequest.usebinary = true;
            ftprequest.usepassive = true;
            ftprequest.keepalive = true;
            ftprequest.method = webrequestmethods.ftp.rename;
            ftprequest.renameto = newfilename;
            ftpresponse = (ftpwebresponse)ftprequest.getresponse();
            ftpresponse.close();
            ftprequest = null;
        }
        catch (exception ex) { console.writeline(ex.tostring()); }
        return;
    }

    public void createdirectory(string newdirectory)
    {
        try
        {
            ftprequest = (ftpwebrequest)webrequest.create(host + "/" + newdirectory);
            ftprequest.credentials = new networkcredential(user, pass);
            ftprequest.usebinary = true;
            ftprequest.usepassive = true;
            ftprequest.keepalive = true;
            ftprequest.method = webrequestmethods.ftp.makedirectory;
            ftpresponse = (ftpwebresponse)ftprequest.getresponse();
            ftpresponse.close();
            ftprequest = null;
        }
        catch (exception ex) { console.writeline(ex.tostring()); }
        return;
    }

    public string getfilecreateddatetime(string filename)
    {
        try
        {
            ftprequest = (ftpwebrequest)ftpwebrequest.create(host + "/" + filename);
            ftprequest.credentials = new networkcredential(user, pass);
            ftprequest.usebinary = true;
            ftprequest.usepassive = true;
            ftprequest.keepalive = true;
            ftprequest.method = webrequestmethods.ftp.getdatetimestamp;
            ftpresponse = (ftpwebresponse)ftprequest.getresponse();
            ftpstream = ftpresponse.getresponsestream();
            streamreader ftpreader = new streamreader(ftpstream);
            string fileinfo = null;
            try { fileinfo = ftpreader.readtoend(); }
            catch (exception ex) { console.writeline(ex.tostring()); }
            ftpreader.close();
            ftpstream.close();
            ftpresponse.close();
            ftprequest = null;
            return fileinfo;
        }
        catch (exception ex) { console.writeline(ex.tostring()); }
        return "";
    }

    public string getfilesize(string filename)
    {
        try
        {
            ftprequest = (ftpwebrequest)ftpwebrequest.create(host + "/" + filename);
            ftprequest.credentials = new networkcredential(user, pass);
            ftprequest.usebinary = true;
            ftprequest.usepassive = true;
            ftprequest.keepalive = true;
            ftprequest.method = webrequestmethods.ftp.getfilesize;
            ftpresponse = (ftpwebresponse)ftprequest.getresponse();
            ftpstream = ftpresponse.getresponsestream();
            streamreader ftpreader = new streamreader(ftpstream);
            string fileinfo = null;
            try { while (ftpreader.peek() != -1) { fileinfo = ftpreader.readtoend(); } }
            catch (exception ex) { console.writeline(ex.tostring()); }
            ftpreader.close();
            ftpstream.close();
            ftpresponse.close();
            ftprequest = null;
            return fileinfo;
        }
        catch (exception ex) { console.writeline(ex.tostring()); }
        return "";
    }

    public string[] directorylistsimple(string directory)
    {
        try
        {
            ftprequest = (ftpwebrequest)ftpwebrequest.create(host + "/" + directory);
            ftprequest.credentials = new networkcredential(user, pass);
            ftprequest.usebinary = true;
            ftprequest.usepassive = true;
            ftprequest.keepalive = true;
            ftprequest.method = webrequestmethods.ftp.listdirectory;
            ftpresponse = (ftpwebresponse)ftprequest.getresponse();
            ftpstream = ftpresponse.getresponsestream();
            streamreader ftpreader = new streamreader(ftpstream);
            string directoryraw = null;
            try { while (ftpreader.peek() != -1) { directoryraw += ftpreader.readline() + "|"; } }
            catch (exception ex) { console.writeline(ex.tostring()); }
            ftpreader.close();
            ftpstream.close();
            ftpresponse.close();
            ftprequest = null;
            try { string[] directorylist = directoryraw.split("|".tochararray()); return directorylist; }
            catch (exception ex) { console.writeline(ex.tostring()); }
        }
        catch (exception ex) { console.writeline(ex.tostring()); }
        return new string[] { "" };
    }

    public string[] directorylistdetailed(string directory)
    {
        try
        {
            ftprequest = (ftpwebrequest)ftpwebrequest.create(host + "/" + directory);
            ftprequest.credentials = new networkcredential(user, pass);
            ftprequest.usebinary = true;
            ftprequest.usepassive = true;
            ftprequest.keepalive = true;
            ftprequest.method = webrequestmethods.ftp.listdirectorydetails;
            ftpresponse = (ftpwebresponse)ftprequest.getresponse();
            ftpstream = ftpresponse.getresponsestream();
            streamreader ftpreader = new streamreader(ftpstream);
            string directoryraw = null;
            try { while (ftpreader.peek() != -1) { directoryraw += ftpreader.readline() + "|"; } }
            catch (exception ex) { console.writeline(ex.tostring()); }
            ftpreader.close();
            ftpstream.close();
            ftpresponse.close();
            ftprequest = null;
            try { string[] directorylist = directoryraw.split("|".tochararray()); return directorylist; }
            catch (exception ex) { console.writeline(ex.tostring()); }
        }
        catch (exception ex) { console.writeline(ex.tostring()); }
        return new string[] { "" };
    }
}

复制代码 代码如下:

ftp ftpclient = new ftp(@"ftp://10.10.10.10/", "user", "password");
ftpclient.upload("etc/test.txt", @"c:\users\metastruct\desktop\test.txt");
ftpclient.download("etc/test.txt", @"c:\users\metastruct\desktop\test.txt");
ftpclient.delete("etc/test.txt");
ftpclient.rename("etc/test.txt", "test2.txt");
ftpclient.createdirectory("etc/test");
string filedatetime = ftpclient.getfilecreateddatetime("etc/test.txt");
console.writeline(filedatetime);
string filesize = ftpclient.getfilesize("etc/test.txt");
console.writeline(filesize);
string[] simpledirectorylisting = ftpclient.directorylistdetailed("/etc");
for (int i = 0; i < simpledirectorylisting.count(); i++) { console.writeline(simpledirectorylisting[i]);
string[] detaildirectorylisting = ftpclient.directorylistdetailed("/etc");
for (int i = 0; i < detaildirectorylisting.count(); i++) { console.writeline(detaildirectorylisting[i]); }
ftpclient = null;