本文整理汇总了C#中ISettingsManager.SetOrAddNewItem方法的典型用法代码示例。如果您正苦于以下问题:C# ISettingsManager.SetOrAddNewItem方法的具体用法?C# ISettingsManager.SetOrAddNewItem怎么用?C# ISettingsManager.SetOrAddNewItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ISettingsManager
的用法示例。
在下文中一共展示了ISettingsManager.SetOrAddNewItem方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Unzip
private static void Unzip(ISettingsManager smng)
{
OutputDebug("Unzip start");
var cancellationToken = smng.GetItem<CancellationToken>(SMNGKEY_CANCELLATION_TOKEN).Value;
var baseDir = smng.GetItem<string>(SMNGKEY_BASE_DIR);
var unzipWorkDir = new DirectoryInfo(Path.Combine(baseDir, "unzipWork"));
if (unzipWorkDir.Exists) {
Directory.Delete(unzipWorkDir.FullName, true);
}
unzipWorkDir.Create();
unzipWorkDir.Refresh();
if (!unzipWorkDir.Exists) {
throw new Exception(string.Format("Can't create directory: {0}", unzipWorkDir.FullName));
}
if (unzipWorkDir.EnumerateFileSystemInfos().Any()) {
throw new Exception(string.Format("Can't delete directory contents: {0}", unzipWorkDir.FullName));
}
var unzip = smng.GetItem<string>(SMNGKEY_UNZIP);
var downloadPath = smng.GetItem<string>(SMNGKEY_DOWNLOAD_PATH);
var startInfo = new ProcessStartInfo(unzip) {
Arguments = string.Format("\"{0}\"", downloadPath),
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
WorkingDirectory = unzipWorkDir.FullName
};
using (var process = new Process()) {
process.StartInfo = startInfo;
DataReceivedEventHandler onDataReceived = delegate(object sender, DataReceivedEventArgs e) {
var data = e.Data;
OutputMessage(data);
};
process.OutputDataReceived += onDataReceived;
process.ErrorDataReceived += onDataReceived;
cancellationToken.ThrowIfCancellationRequested();
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
var exitCode = process.ExitCode;
if (exitCode != 0) {
throw new Exception(string.Format("Unzip returned error: {0}", exitCode));
}
}
var unzippedDir = Path.Combine(unzipWorkDir.FullName, "chrome-win32");
if (!Directory.Exists(unzippedDir)) {
throw new Exception(string.Format("Unzip failed, zip: {0}, destination: {1}", downloadPath, unzippedDir));
}
smng.SetOrAddNewItem(SMNGKEY_UNZIPPED_DIR, unzippedDir);
OutputDebug("Unzip end");
}
示例2: GetRevision
private static void GetRevision(ISettingsManager smng)
{
OutputDebug("GetRevision start");
var cancellationToken = smng.GetItem<CancellationToken>(SMNGKEY_CANCELLATION_TOKEN).Value;
var client = GetHttpClient(smng);
var hashUrl = smng.GetItem<Uri>(SMNGKEY_HASH_URL);
var revision = client.GetAsync(hashUrl, cancellationToken).Result.Content.ReadAsStringAsync().Result;
var baseDir = smng.GetItem<string>(SMNGKEY_BASE_DIR);
var pattern = string.Format("chrome-win32_*_{0}.zip", revision);
bool isLatest;
if (Directory.EnumerateFiles(baseDir, pattern).Any()) {
isLatest = true;
}
else {
isLatest = false;
}
smng.SetOrAddNewItem(SMNGKEY_IS_LATEST, isLatest);
smng.SetOrAddNewItem(SMNGKEY_REVISION, revision);
OutputDebug("GetRevision end");
}
示例3: 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;
}
}
示例4: 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;
}
示例5: 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");
}
示例6: 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");
}