本文整理汇总了C#中ConfigFile.FindConfigSection方法的典型用法代码示例。如果您正苦于以下问题:C# ConfigFile.FindConfigSection方法的具体用法?C# ConfigFile.FindConfigSection怎么用?C# ConfigFile.FindConfigSection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConfigFile
的用法示例。
在下文中一共展示了ConfigFile.FindConfigSection方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetBuildServerCredentials
public IBuildServerCredentials GetBuildServerCredentials(IBuildServerAdapter buildServerAdapter, bool useStoredCredentialsIfExisting)
{
lock (buildServerCredentialsLock)
{
IBuildServerCredentials buildServerCredentials = new BuildServerCredentials { UseGuestAccess = true };
const string CredentialsConfigName = "Credentials";
const string UseGuestAccessKey = "UseGuestAccess";
const string UsernameKey = "Username";
const string PasswordKey = "Password";
using (var stream = GetBuildServerOptionsIsolatedStorageStream(buildServerAdapter, FileAccess.Read, FileShare.Read))
{
if (stream.Position < stream.Length)
{
var protectedData = new byte[stream.Length];
stream.Read(protectedData, 0, (int)stream.Length);
try
{
byte[] unprotectedData = ProtectedData.Unprotect(protectedData, null,
DataProtectionScope.CurrentUser);
using (var memoryStream = new MemoryStream(unprotectedData))
{
ConfigFile credentialsConfig = new ConfigFile("", false);
using (var textReader = new StreamReader(memoryStream, Encoding.UTF8))
{
credentialsConfig.LoadFromString(textReader.ReadToEnd());
}
ConfigSection section = credentialsConfig.FindConfigSection(CredentialsConfigName);
if (section != null)
{
buildServerCredentials.UseGuestAccess = section.GetValueAsBool(UseGuestAccessKey,
true);
buildServerCredentials.Username = section.GetValue(UsernameKey);
buildServerCredentials.Password = section.GetValue(PasswordKey);
if (useStoredCredentialsIfExisting)
{
return buildServerCredentials;
}
}
}
}
catch (CryptographicException)
{
// As per MSDN, the ProtectedData.Unprotect method is per user,
// it will throw the CryptographicException if the current user
// is not the one who protected the data.
// Set this variable to false so the user can reset the credentials.
useStoredCredentialsIfExisting = false;
}
}
}
if (!useStoredCredentialsIfExisting)
{
buildServerCredentials = ShowBuildServerCredentialsForm(buildServerAdapter.UniqueKey, buildServerCredentials);
if (buildServerCredentials != null)
{
ConfigFile credentialsConfig = new ConfigFile("", true);
ConfigSection section = credentialsConfig.FindOrCreateConfigSection(CredentialsConfigName);
section.SetValueAsBool(UseGuestAccessKey, buildServerCredentials.UseGuestAccess);
section.SetValue(UsernameKey, buildServerCredentials.Username);
section.SetValue(PasswordKey, buildServerCredentials.Password);
using (var stream = GetBuildServerOptionsIsolatedStorageStream(buildServerAdapter, FileAccess.Write, FileShare.None))
{
using (var memoryStream = new MemoryStream())
{
using (var textWriter = new StreamWriter(memoryStream, Encoding.UTF8))
{
textWriter.Write(credentialsConfig.GetAsString());
}
var protectedData = ProtectedData.Protect(memoryStream.ToArray(), null, DataProtectionScope.CurrentUser);
stream.Write(protectedData, 0, protectedData.Length);
}
}
return buildServerCredentials;
}
}
return null;
}
}