zoukankan      html  css  js  c++  java
  • 怎么用C#在ftp服务器(远程)上建立文件夹?

    现在由于项目的需要,需要在ftp服务器上根据情况建立文件夹,然后把本地文件上传上去,全部用C#实现,现在就在想,上传很简单,怎么在ftp服务器上远程建立文件夹呢?权限是管理员用户权限,应该是最大的了,呵呵。哪位大虾给点提示代码看看,成功立马给分,谢谢!
     
     回复内容
    【FarAwayFromHome】:
    在网上找了些资料,有人说可以给ftp服务器发送命令,然后让ftp服务器去建立文件夹,不知是否可行,具体的方案还没有找到,还有更好的方案不?

    【yangfan369】:
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
      request.Credentials = ftpCredential;
     request.Method = WebRequestMethods.Ftp.MakeDirectory;

    【yangfan369】:
    NetworkCredential ftpCredential=new NetworkCredential(userName,passWord);
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
      request.Credentials = ftpCredential;
     request.Method = WebRequestMethods.Ftp.MakeDirectory;

    --忘了一句,呵呵
    具体参见FtpWebRequest在MSDN中的例子


    【michael556cdj】:
    public static bool DeleteFileOnServer(Uri serverUri)
    {
        // The serverUri parameter should use the ftp:// scheme.
        // It contains the name of the server file that is to be deleted.
        // Example: ftp://contoso.com/someFile.txt.
        //
       
        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            return false;
        }
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
        request.Method = WebRequestMethods.Ftp.DeleteFile;
     
        FtpWebResponse response = (FtpWebResponse) request.GetResponse();
        Console.WriteLine("Delete status: {0}",response.StatusDescription); 
        response.Close();
        return true;
    }

    【FarAwayFromHome】:
    To:yangfan369(扬帆远行)
    谢谢你的回答,我调试了一下,还是不行啊,执行完了,服务器上没有任何反应哦!

    【yangfan369】:
    少了一句,不好意思:
    request.GetResponse();

    【yangfan369】:
    或者:
    string rsult = ((FtpWebResponse)request.GetResponse()).StatusDescription;
    可以看到返回结果

    【FarAwayFromHome】:
    非常感谢 yangfan369 分已给出!
    但是又出现一个问题(如果有时间可以回答一下,我会重新发出另外一个贴的):
    在服务器上建立目录是解决了,但是第一次建立好的目录,第二次上传的时候要删除掉,删除方法是找到了,用 WebRequestMethods.Ftp.RemoveDirectory;但是问题出来了,不知道ftp的目录情况啊,要知道目录才能删除,我想到了用 WebRequestMethods.Ftp.ListDirectoryDetails; 可是返回的目录信息怎么获取到呢?用response 无法获取啊,希望能给点参考意见,万分感谢!
    虚无无所不在,无所不容;虚无包括一切,没有相反,没有对立,也没有排斥,因为它是万物之源,认识虚无的人都充满生命力和爱心!
  • 相关阅读:
    初学者一些常用的SQL语句(一)
    java小知识
    ArrayList底层实现原理
    JVM原理
    一个简单的登陆注册页面(希望可以帮到您)
    数据结构
    C++/C
    C的函数指针与指针函数
    函数指针与指针函数
    对数据库通用性的更新操作(ssh)
  • 原文地址:https://www.cnblogs.com/sunjie9606/p/1251084.html
Copyright © 2011-2022 走看看