public void ExportToFTP(string fullfilename,string filename)
        {
            DateTime dt = DateTime.Now;
            string subFolder = dt.Month + "-" + dt.Day + "-" + dt.Year;            string dt = @"ftp://www.aaa.com/" + subFolder + "/" +filename;
            
我就是想用FtpWebRequest实现上传到特定文件夹,文件夹为日期(3-21-2006),明天就把文件上传到
ftp://www.aaa.com/3-22-2006/中,现在我不知道该如何判断是否存在这个日期文件夹,如果没有我该如何创建???            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(dt);
            
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential("upload", "6TXJAmrG");
            StreamReader sourceStream = new StreamReader(fullfilename);
            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();            request.ContentLength = fileContents.Length;
            Stream requestStream = request.GetRequestStream();            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();
        }

解决方案 »

  1.   

    string subFolder = dt.Month + "-" + dt.Day + "-" + dt.Year;
    string dt = @"ftp://www.aaa.com/" + subFolder + "/" +filename;
    比如今天是3-21-2006日,那文件就应该上传到ftp://www.aaa.com/3-21-2006中,现在服务器中没有3-21-2006文件夹,运行出错!!!
    也就是没有建立3-21-2006文件夹,现在我不知道该如何判断是否存在这个日期文件夹,如果没有我该如何创建???
      

  2.   

    using System.IO;
    public void CreateSubDirectory(string path)//---------创建目录
    {
    DirectoryInfo directoryInfo=new DirectoryInfo(path);
    if(directoryInfo.Exists!=true)
    directoryInfo.Create();
    directoryInfo=null;
    }
      

  3.   

    using System.IO;
    public void CreateSubDirectory(string path)//---------创建目录
    {
     DirectoryInfo directoryInfo=new DirectoryInfo(path);
     if(directoryInfo.Exists!=true)
       directoryInfo.Create();
     directoryInfo=null;
    }
    path可以是ftp://www.aaa.com/3-16-2006/吗?
      

  4.   

    想问问你用的是2005哪个版本,我用FtpWebRequest类时,直接用MSDN上的代码都出错“the underlying connection was closed: The server committed a protocol violation.”,索性在http://forums.microsoft.com上搜了搜,说是该类有BUG,不知现在解决了没?你的没出现这个问题吧
      

  5.   

    <---make!还没有用过这个控件,做个标记.
      

  6.   

    你可以先请求FTP上这个文件夹中的一个文件,根据获取的流大小,判断文件是否存在,是则上传,否则就创建文件夹..下面是MSDN上的代码 ..参考下给你:===========================================static void Upload(string fileName, string uploadUrl)
            {
                Stream requestStream = null;
                FileStream fileStream = null;
                FtpWebResponse uploadResponse = null;
                try
                {
                    FtpWebRequest uploadRequest =
                        (FtpWebRequest)WebRequest.Create(uploadUrl);
                    uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;                // UploadFile is not supported through an Http proxy
                    // so we disable the proxy for this request.
                    uploadRequest.Proxy = null;                requestStream = uploadRequest.GetRequestStream();
                    fileStream = File.Open(fileName, FileMode.Open);                byte[] buffer = new byte[1024];
                    int bytesRead;
                    while (true)
                    {
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                            break;
                        requestStream.Write(buffer, 0, bytesRead);
                    }                // The request stream must be closed before getting 
                    // the response.
                    requestStream.Close();                uploadResponse =
                        (FtpWebResponse)uploadRequest.GetResponse();
                    Console.WriteLine("Upload complete.");
                }
                catch (UriFormatException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (WebException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (uploadResponse != null)
                        uploadResponse.Close();
                    if (fileStream != null)
                        fileStream.Close();
                    if (requestStream != null)
                        requestStream.Close();
                }
            }
      

  7.   

    if (!Directory.Exists(path))
    {
     Directory.CreateDirectory(path);
    }