我有这段代码,我从sql database生成xml文件:
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文件中没有任何代码......
答案 0 :(得分:1)
在strFileName
函数中写完GenerovaniVse
后,您可能需要关闭{{1}}。