好的,所以我想使用 c# 2010 向我的 drivehq ftp 服务器发送一个文件.dat 文件到 ftp 没有损坏请帮助
文件的原始大小与上传时不同并且文件已损坏
我的源代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string userName = Environment.UserName;
if (File.Exists(@"C:\Users\" + userName + @"\AppData\Roaming\minefarm\stats.dat"))
{
Console.WriteLine("The file exists.");
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.drivehq.com/btc/" + userName + (GetPublicIpAddress() + ".dat"));
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("user", "pass");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(@"C:\Users\" + userName + @"\AppData\Roaming\minefarm\stats.dat"));
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();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
System.Threading.Thread.Sleep(5000);
}
}
private static string GetPublicIpAddress()
{
var request = (HttpWebRequest)WebRequest.Create("http://ifconfig.me");
request.UserAgent = "curl"; // this simulate curl linux command
string publicIPAddress;
request.Method = "GET";
using (WebResponse response = request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
publicIPAddress = reader.ReadToEnd();
}
}
return publicIPAddress.Replace("\n", "");
}
}
}