Skip to content

Commit

Permalink
Changed the Encoding property of System.Net.FtpClient to allow manual…
Browse files Browse the repository at this point in the history
…ly setting your own text encoding.
  • Loading branch information
jptrosclair committed Feb 13, 2014
1 parent ac9b5bf commit 399b7bf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
33 changes: 26 additions & 7 deletions System.Net.FtpClient/FtpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,33 @@ private set {
}
}

Encoding m_overrideEncoding = null;

Encoding m_textEncoding = Encoding.ASCII;
/// <summary>
/// Gets the text encoding being used when talking with the server. The default
/// Gets or sets the text encoding being used when talking with the server. The default
/// value is Encoding.ASCII however upon connection, the client checks
/// for UTF8 support and if it's there this property is switched over to
/// Encoding.UTF8.
/// Encoding.UTF8. Manually setting this value overrides automatic detection
/// based on the FEAT list; if you change this value it's always used
/// regardless of what the server advertises, if anything.
/// </summary>
[FtpControlConnectionClone]
public Encoding Encoding {
get {
return m_textEncoding;
}
private set {
m_textEncoding = value;
set {
try {
m_lock.WaitOne();

if (IsConnected)
m_textEncoding = value;
m_overrideEncoding = value;
}
finally {
m_lock.ReleaseMutex();
}
}
}

Expand Down Expand Up @@ -762,7 +776,10 @@ public virtual void Connect() {
if (Credentials == null)
throw new FtpException("No credentials have been specified");

m_textEncoding = Encoding.ASCII;
if (m_overrideEncoding == null)
m_textEncoding = Encoding.ASCII;
else
m_textEncoding = m_overrideEncoding;

if (!IsClone) {
m_caps = FtpCapability.NONE;
Expand Down Expand Up @@ -821,8 +838,10 @@ public virtual void Connect() {
// If the server supports UTF8 it should already be enabled and this
// command should not matter however there are conflicting drafts
// about this so we'll just execute it to be safe.
Execute("OPTS UTF8 ON");
m_textEncoding = Encoding.UTF8;
if (m_overrideEncoding == null || m_overrideEncoding == Encoding.UTF8) {
Execute("OPTS UTF8 ON");
m_textEncoding = Encoding.UTF8;
}
}

FtpTrace.WriteLine("Text encoding: " + m_textEncoding.ToString());
Expand Down
2 changes: 1 addition & 1 deletion System.Net.FtpClient/IFtpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public interface IFtpClient : IDisposable {
/// <summary>
/// Added for the MoQ unit testing framework
/// </summary>
Encoding Encoding { get; }
Encoding Encoding { get; set; }

/// <summary>
/// Added for the MoQ unit testing framework
Expand Down
1 change: 1 addition & 0 deletions Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static void Main(string[] args) {
cl.EncryptionMode = FtpEncryptionMode.None;
cl.ValidateCertificate += new FtpSslValidation(cl_ValidateCertificate);
cl.DataConnectionType = (FtpDataConnectionType)i;
//cl.Encoding = System.Text.Encoding.Default;
cl.Connect();
Upload(cl);
Download(cl);
Expand Down

0 comments on commit 399b7bf

Please sign in to comment.