.NET轉碼Unicode to Big5

C#預設的轉檔格式為Unicode,工作上需要上傳一個檔案到Big5的編碼環境

如何上傳一個檔案到Big5環境by c#

1、先宣告一個ftpWebRequest,method設定為uploadFile,使用binary的方式傳輸

// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://123.0.0.1");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = false;
request.UseBinary = true;
request.Credentials = new NetworkCredential(UserID, Password);

2、宣告一個streamReader讀取檔案,c#預設的編碼方式為Unicode,所以編碼格式改為950,950為繁體中文big5編碼格式,其它格式如      http://www.lingoes.net/en/translator/codepage.htm

// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(UploadFile, Encoding.GetEncoding(950));
byte[] fileContents = Encoding.Default.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;

3、宣告stream 接收webftprequest

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
response.Close();