本文整理汇总了C#中ISettingsManager.TryGetItem方法的典型用法代码示例。如果您正苦于以下问题:C# ISettingsManager.TryGetItem方法的具体用法?C# ISettingsManager.TryGetItem怎么用?C# ISettingsManager.TryGetItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ISettingsManager
的用法示例。
在下文中一共展示了ISettingsManager.TryGetItem方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetProxySettings
private static WebProxy GetProxySettings(ISettingsManager smng)
{
OutputDebug("GetProxySettings start");
string proxyHost;
if (smng.TryGetItem(SMNGKEY_PROXY_HOST, out proxyHost)) {
int proxyPort;
if (!smng.TryGetItem(SMNGKEY_PROXY_PORT, out proxyPort)) {
proxyPort = 8080;
}
var proxy = new WebProxy(proxyHost, proxyPort);
string proxyUser;
if (smng.TryGetItem(SMNGKEY_PROXY_USER, out proxyUser)) {
string proxyPassword;
if (!smng.TryGetItem(SMNGKEY_PROXY_PASSWORD, out proxyPassword)) {
proxyPassword = ReceivePassword();
smng.SetOrAddNewItem(SMNGKEY_PROXY_PASSWORD, proxyPassword);
}
var credential = new NetworkCredential(proxyUser, proxyPassword);
proxy.Credentials = credential;
}
OutputDebug("GetProxySettings end");
return proxy;
}
else {
OutputDebug("GetProxySettings end");
return null;
}
}
示例2: GetHttpClient
private static HttpClient GetHttpClient(ISettingsManager smng)
{
OutputDebug("GetHttpClient start");
HttpClient client;
lock (httpClientSetupLock) {
if (!smng.TryGetItem(SMNGKEY_HTTP_CLIENT, out client)) {
var clientHandler = new HttpClientHandler();
var proxy = GetProxySettings(smng);
clientHandler.Proxy = proxy;
client = new HttpClient(clientHandler);
smng.SetOrAddNewItem(SMNGKEY_HTTP_CLIENT, client);
}
}
OutputDebug("GetHttpClient end");
return client;
}
示例3: DownloadZip
private static void DownloadZip(ISettingsManager smng)
{
OutputDebug("DownloadZip start");
var cancellationToken = smng.GetItem<CancellationToken>(SMNGKEY_CANCELLATION_TOKEN).Value;
var revision = smng.GetItem<string>(SMNGKEY_REVISION);
string zipUrlTemplate = smng.GetItem<string>(SMNGKEY_ZIP_URL_TEMPLATE);
var zipUrlStr = zipUrlTemplate.Replace("{hash}", revision);
var zipUrl = new Uri(zipUrlStr);
var client = GetHttpClient(smng);
cancellationToken.ThrowIfCancellationRequested();
var dlTask = client.GetAsync(zipUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
long contentLength;
var baseDir = smng.GetItem<string>(SMNGKEY_BASE_DIR);
var timestamp = DateTime.Now.ToString("yyyyMMddHHmmssfff");
var downloadFile = string.Format("chrome-win32_{0}_{1}.zip", timestamp, revision);
var downloadPath = Path.Combine(baseDir, downloadFile);
using (var response = dlTask.Result)
using (var content = response.Content) {
contentLength = content.Headers.ContentLength ?? -1;
OutputMessage(string.Format("Total {0} bytes", contentLength));
var percentage = 0L;
var prevPercentage = -1L;
var current = 0L;
smng.SetOrAddNewItem(SMNGKEY_DOWNLOAD_PATH, downloadPath);
cancellationToken.ThrowIfCancellationRequested();
using (var input = content.ReadAsStreamAsync().Result)
using (var output = File.Open(downloadPath, FileMode.Create, FileAccess.Write, FileShare.None)) {
var buffer = new byte[8192];
int count;
while ((count = input.Read(buffer, 0, buffer.Length)) > 0) {
cancellationToken.ThrowIfCancellationRequested();
output.Write(buffer, 0, count);
current += count;
percentage = current * 100L / contentLength;
if (prevPercentage < percentage) {
prevPercentage = percentage;
lock (consoleWriteLock) {
Console.Write(string.Format("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bdownloading {0,3}%", percentage));
}
}
}
OutputMessage(string.Empty);
}
}
var downloadFileInfo = new FileInfo(downloadPath);
long fileLength = downloadFileInfo.Length;
if (fileLength != contentLength) {
throw new Exception(string.Format("Incomplete download, expected: {0}, actual: {1}", contentLength, fileLength));
}
if (!downloadFileInfo.Exists) {
throw new Exception(string.Format("Download zip failed: {0}", downloadPath));
}
// ^chrome-win32_(?<timestamp>\d+)_[0-9a-f]+\.zip$
var zipPattern = "^chrome-win32_(?<timestamp>\\d+)_[0-9a-f]+\\.zip$";
int backupCycle;
if (!smng.TryGetItem(SMNGKEY_BACKUP_CYCLE, out backupCycle)) {
backupCycle = int.MaxValue;
}
var oldVersionZipQuery = Directory.GetFiles(baseDir)
.Select(x => Path.GetFileName(x))
.Select(x => Regex.Match(x, zipPattern))
.Where(m => m.Success)
.OrderByDescending(m => m.Groups["timestamp"].Value)
.Skip(backupCycle)
.Select(m => Path.Combine(baseDir, m.Value));
foreach (string backupToDelete in oldVersionZipQuery) {
if (File.Exists(backupToDelete)) {
File.Delete(backupToDelete);
}
if (File.Exists(backupToDelete)) {
cancellationToken.ThrowIfCancellationRequested();
throw new Exception(string.Format("Error: delete old zip: {0}", backupToDelete));
}
}
OutputDebug("DownloadZip end");
}
示例4: CheckSuspended
private static void CheckSuspended(ISettingsManager smng)
{
OutputDebug("CheckSuspended start");
bool isSuspended;
string suspendFileName;
if (smng.TryGetItem(SMNGKEY_SUSPENDED_FILE_PATH, out suspendFileName)) {
string execDir = GetExecDir();
string suspendFilePath = Path.Combine(execDir, suspendFileName);
isSuspended = File.Exists(suspendFilePath);
}
else {
isSuspended = false;
}
smng.SetOrAddNewItem(SMNGKEY_IS_SUSPENDED, isSuspended);
OutputDebug("CheckSuspended end");
}
示例5: BackupProfileDir
private static void BackupProfileDir(ISettingsManager smng)
{
OutputDebug("BackupProfileDir start");
string ffc;
string profileDir;
string profileBackup;
int profileBackupCycle;
if (!smng.TryGetItem<string>("ffc", out ffc)) {
return;
}
if (!smng.TryGetItem<string>("profileDir", out profileDir)) {
return;
}
if (!smng.TryGetItem<string>("profileBackup", out profileBackup)) {
return;
}
if (!smng.TryGetItem<int>("profileBackupCycle", out profileBackupCycle)) {
return;
}
else if (profileBackupCycle < 1) {
return;
}
if (File.Exists(ffc) && Directory.Exists(profileDir) && Directory.Exists(profileBackup)) {
OutputMessage("Backup profile directory");
var src = profileDir.TrimEnd('\\', '/');
var dstNewDir = string.Format("ChromiumUserData_{0}", DateTime.Now.ToString("yyyyMMddHHmmssfff"));
var dst = Path.Combine(profileBackup, dstNewDir);
OutputMessage(string.Format(" {0} -> {1}", src, dst));
var startInfo = new ProcessStartInfo(ffc) {
Arguments = string.Format("\"{0}\" /to:\"{1}\" /ed /md /ft:15", src, dst),
CreateNoWindow = false,
UseShellExecute = false,
};
var cancellationToken = smng.GetItem<CancellationToken>(SMNGKEY_CANCELLATION_TOKEN).Value;
cancellationToken.ThrowIfCancellationRequested();
using (Process process = new Process()) {
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
int exitCode = process.ExitCode;
if (exitCode != 0) {
throw new Exception(string.Format("ffc has exited with code {0}", exitCode));
}
}
if (!Directory.Exists(dst)) {
throw new Exception(string.Format("Backup profile dir filed, dst: {0}", dst));
}
var oldProfileBackupQuery = Directory.GetDirectories(profileBackup)
.Select(x => Path.GetFileName(x))
.Where(x => Regex.IsMatch(x, "^ChromiumUserData_(\\d{17})$")) // ^ChromiumUserData_(\d{17})$
.OrderByDescending(x => x)
.Skip(profileBackupCycle)
.Select(x => Path.Combine(profileBackup, x));
bool isFirst = true;
foreach (string oldProfileBackup in oldProfileBackupQuery) {
if (isFirst) {
isFirst = false;
OutputMessage("Remove old profile backups");
}
OutputMessage(string.Format(" {0}", oldProfileBackup));
Directory.Delete(oldProfileBackup, true);
}
}
OutputDebug("BackupProfileDir end");
}
示例6: BackupExeDir
private static void BackupExeDir(ISettingsManager smng)
{
OutputDebug("BackupExeDir start");
var cancellationToken = smng.GetItem<CancellationToken>(SMNGKEY_CANCELLATION_TOKEN).Value;
cancellationToken.ThrowIfCancellationRequested();
var baseDir = smng.GetItem<string>(SMNGKEY_BASE_DIR);
var currentBackupNum = Directory.GetDirectories(baseDir, "chrome-win32~*")
.Select(x => int.Parse(Regex.Match(x, "chrome-win32~(\\d+)").Groups[1].Value))
.OrderByDescending(x => x)
.First();
var backupNum = currentBackupNum + 1;
var backupDir = Path.Combine(baseDir, string.Format("chrome-win32~{0}", backupNum));
var appDir = Path.Combine(baseDir, "chrome-win32");
Directory.Move(appDir, backupDir);
if (Directory.Exists(appDir)) {
throw new Exception(string.Format("Move directory failed, src: {0}", appDir));
}
if (!Directory.Exists(backupDir)) {
throw new Exception(string.Format("Move directory failed, dst: {1}", backupDir));
}
int backupCycle;
if (!smng.TryGetItem(SMNGKEY_BACKUP_CYCLE, out backupCycle)) {
backupCycle = int.MaxValue;
}
var oldExeDirQuery = Directory.GetDirectories(baseDir)
.Select(x => Path.GetFileName(x))
.Where(x => Regex.IsMatch(x, "^chrome-win32~(?<num>\\d+)$")) // ^chrome-win32~(?<num>\d+)$
.OrderByDescending(x => int.Parse(Regex.Match(x, "^chrome-win32~(?<num>\\d+)$").Groups["num"].Value))
.Skip(backupCycle)
.Select(x => Path.Combine(baseDir, x));
foreach (string backupToDelete in oldExeDirQuery) {
OutputDebug(string.Format("Deleting {0}", Path.GetFileName(backupToDelete)));
Directory.Delete(backupToDelete, true);
}
OutputDebug("BackupExeDir end");
}