2013-08-23 58 views
0

我有這樣的代碼,其中i生成SQL數據庫的XML文件:創建FTP上的文件名相同的本地名稱

int counter; 
    int counter2; 
private void GenerovaniVse() 
{ 
    SqlConnection con = new SqlConnection("Data Source=****"); 
    string strSQL = "select w.KOD_....";     
    SqlDataAdapter dt = new SqlDataAdapter(strSQL, con); 
    DataSet ds = new DataSet("ITEMS"); 
    dt.Fill(ds, "ITEM"); 
    FileStream strFileName = File.Create(@"C:\\Users\\L\\Desktop\\" + (counter++) + ".xml"); 
    XmlWriterSettings settings = new XmlWriterSettings(); 
    settings.Encoding = Encoding.Unicode; 

    using (XmlWriter xmlWriter = XmlWriter.Create(strFileName, settings)) 
    { 
     ds.WriteXml(xmlWriter); 
     xmlWriter.Close(); 
    } 
} 

而且從上傳此代碼文件到FTP:

private void Upload() 
{ 
    FileInfo f = new FileInfo("C:\\Users\\L\\Desktop\\" + (counter2) + ".xml"); 
    long original_vel = f.Length; 
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(*****); 
    request.Method = WebRequestMethods.Ftp.GetFileSize; 
    request.Method = WebRequestMethods.Ftp.UploadFile; 
    request.Credentials = new NetworkCredential("***", "***"); 

    StreamReader sourceStream = new StreamReader(??????); 
    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 
    sourceStream.Close(); 
    request.ContentLength = fileContents.Length; 
    long ftp_vel = request.ContentLength; //velikost souboru na ftp  
    Stream requestStream = request.GetRequestStream(); 
    requestStream.Write(fileContents, 0, fileContents.Length); 
    requestStream.Close(); 
    FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
       counter2++; 
       response.Close();       
} 

問題在:strmfile.Close(); 但現在當我將xml複製到ftp時,ftp上的文件具有較小的原始xml ...原始xml上有Unicode和ftp文件沒有任何代碼...

回答

1

您可能需要關閉在完成寫入GenerovaniVse函數之後,您的strFileName

+0

是的,沒錯! :)但現在當我將xml複製到ftp時,ftp上的文件具有較小的原始xml ...並且在原始xml上我有Unicode並且在ftp文件中沒有任何代碼...:/ – Kate

+0

我的猜測是文件是以文本而不是二進制文件的形式發送的,請嘗試通過設置「UseBinary」屬性來強制它 - http://msdn.microsoft.com/zh-cn/library/system.net.ftpwebrequest.usebinary.aspx –

相關問題