namespace StockSharp.Community
{
using System;
using System.IO;
using Ecng.Serialization;
///
/// Extension class.
///
public static class ServerCredentialsExtensions
{
///
/// The StockSharp folder in location.
///
public static string StockSharpFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "StockSharp");
private const string _credentialsFile = "credentials.xml";
///
/// Try load credentials from .
///
/// The class that contains a login and password to access the services https://stocksharp.com .
/// if the specified credentials was loaded successfully, otherwise, .
public static bool TryLoadCredentials(this ServerCredentials credentials)
{
if (credentials == null)
throw new ArgumentNullException(nameof(credentials));
var file = Path.Combine(StockSharpFolder, _credentialsFile);
if (!File.Exists(file))
return false;
credentials.Load(new XmlSerializer().Deserialize(file));
return true;
}
///
/// Save the credentials to .
///
/// The class that contains a login and password to access the services https://stocksharp.com .
public static void SaveCredentials(this ServerCredentials credentials)
{
if (credentials == null)
throw new ArgumentNullException(nameof(credentials));
credentials.SaveCredentials(credentials.AutoLogon);
}
///
/// Save the credentials to .
///
/// The class that contains a login and password to access the services https://stocksharp.com .
/// Save password.
public static void SaveCredentials(this ServerCredentials credentials, bool savePassword)
{
if (credentials == null)
throw new ArgumentNullException(nameof(credentials));
var clone = credentials;
if (!savePassword)
clone.Password = null;
var file = Path.Combine(StockSharpFolder, _credentialsFile);
new XmlSerializer().Serialize(clone.Save(), file);
}
}
}