在用户设置路径构建中获取证据eid和hash的值

问题描述 投票:0回答:2

默认情况下win应用程序的用户设置存储在以下目录中

远景/7

C:\Users\<userId>\AppData\Local\Application Data\<companyname>\appdomainname>_<eid>_<hash>\<verison>

XP

C:\Documents and Settings>\<username>\[Local Settings\]Application Data\<companyname>\appdomainname>_<eid>_<hash>\<verison>

我需要知道如何获取 eidhash 的值。

我试图从

AppDomain.CurrentDomain.Evidence
获取该信息,然后检查从 GetHostEnumerator() 获取的值,但它们不适合实际的目录路径值。

例如我有以下值

Some.exe_StrongName_fymjkuum4xrf5aopfjlsem3elhvjbgag

但是我从代码中收到的信息是

<StrongName version="1"
Key="002400000480000094000000060200000024000052534131000400000100010027BFE9320943DDB4271C78B6B890E7BF02ECAA65585E252D8FBF07888FAAC54D8F8EF25C65061D4F8B904699701BF437F5A69BBDB6A496B989F8FD96853E43C621A84C187AF9EA90C0DAF7F32134A3BD42E94023DBB601C864CA1FF0B5E520CD090A4B90EDB1F95628F750316DBCC9593603E033D72FD67F2707D2670A2D1EB2"
Name="Some"
Version="0.0.0.0"/>

<System.Security.Policy.Url version="1">
<Url>file:///R:/Some/Some.Utilities/bin/Debug/Some.EXE</Url>
</System.Security.Policy.Url>

<System.Security.Policy.Zone version="1">
<Zone>MyComputer</Zone>
</System.Security.Policy.Zone>

<System.Security.Policy.Hash version="2">
<hash algorithm="SHA1"
value="8B19FB026023FE0C239D96EECDDC0266D36B415B"/>
<hash algorithm="SHA256"
value="46AA666701E20EF698ABE20F60CD52162BD0D7B72B43D1B1EB82826E525ACE73"/>
<hash algorithm="MD5"
value="244B4EA2E084F98345FE56FB4460A436"/>
</System.Security.Policy.Hash>

顺便说一句,我的集会正在签名。也许这是我的密钥的值?

c# winforms settings
2个回答
3
投票

您可以使用以下代码行获取应用程序默认保存设置文件的位置。从中您可以提取 eid 和哈希值。

string configPath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;

0
投票

这可能会碰触旧线程,但我想我应该添加到此。

我试图弄清楚如何重建文件路径,最终放弃了。

我的用例是用户配置文件有时会损坏并导致异常。他们为何/如何腐败尚不清楚;用户经常声称他们没有修改它们,也许在 XML 写入过程中发生了奇怪的崩溃?

解决方案;尝试打开配置文件,如果失败,捕获错误,从消息中提取配置文件路径,然后将其删除。

public static class ConfigFunctions
{
public const string CONFIG_EXT = ".CONFIG";

/// <summary>
/// Check that the config file can be opened - by default the localappdata user.config for entry assembly.
/// If it fails we may attempt to auto fix it (delete corrupt config file). For my use this was acceptable - but you may want to be more cautious.
/// Note: this *must* be run from the original entry assembly. You cannot access the config from a different EXE. If the EXE file is moved and executed elsewhere it will *also* be different
/// Note: I tried but could not manually create the EXE_URL_XXXX section of the file path of local user.config (I believe it's a URL to FILE PATH that's been hashed SHA1 Base36 but not sure).
/// </summary>
public static bool TryOpenConfig(System.Configuration.ConfigurationUserLevel configUserLevel = System.Configuration.ConfigurationUserLevel.PerUserRoamingAndLocal, bool attemptAutoFix = true)
{
    try
    {
    string errorMsg;
    var configFilePath = GetConfigFilePath(configUserLevel, attemptAutoFix, out errorMsg); // Try and get the config file path
    var errorOccurred = !string.IsNullOrWhiteSpace(errorMsg); // If we got an error message then an error occurred
    // Log config file path and/or error message?
    return !errorOccurred;
    }
    catch (Exception exc)
    {
    // Log this unexpected exception?
    throw; // Rethrow it preserving call stack
    }
}

/// <summary>
/// We try and get the config file path by opening the exe configuration.
/// If this fails, we try and get it from the config exception message.
/// We optionally delete the config file (helps auto fix problems)
/// We return the path, and if it errored or not (irrespective if we fixed it via delete).
/// </summary>
private static string GetConfigFilePath(System.Configuration.ConfigurationUserLevel configUserLevelLocal, bool deleteOnError, out string errorMsg)
{
    errorMsg = null; // Set this initially
    try
    {
    var config = System.Configuration.ConfigurationManager.OpenExeConfiguration(configUserLevelLocal); // Try and open using ConfigurationManager
    return config?.FilePath; // Return the file path from it (i.e. we opened successfully)
    }
    catch (System.Configuration.ConfigurationException configExc) // We had a configuration exc
    {
    errorMsg = configExc?.Message; // Set the message so we can return it but don't rethrow it - it's an "acceptable error"
    var configFilePathFromExc = configExc?.GetFilePath(); // Use our extension method to get the file path from this
    if (deleteOnError // If we want to delete on error (i.e. auto fix corrupt file) and the file exists...
        && !string.IsNullOrWhiteSpace(configFilePathFromExc)
        && System.IO.File.Exists(configFilePathFromExc))
    {
        System.IO.File.Delete(configFilePathFromExc); // Try and delete it (note, may throw exc)
    }
    return configFilePathFromExc;
    }
}

/// <summary>
/// We can try and get the file path from the exception message via some string manipulation.
/// </summary>
private static string GetFilePath(this System.Configuration.ConfigurationException configExc)
{
    var configExcMsg = configExc?.Message; // Get the error message
    var configExcMsgSplitOnBrackets = configExcMsg?.Split(new[] { '(', ')' }); // Split it on brackets (i.e. the config file is in here)
    var configExcMsgSubStrHasConfigExt = configExcMsgSplitOnBrackets?.LastOrDefault(s => s.IndexOf(CONFIG_EXT, StringComparison.OrdinalIgnoreCase) >= 0);
    if (!string.IsNullOrWhiteSpace(configExcMsgSubStrHasConfigExt))
    {
    configExcMsgSubStrHasConfigExt = configExcMsgSubStrHasConfigExt?.Trim(); // Trim it just incase leading space.
    var configExtPos = configExcMsgSubStrHasConfigExt?.LastIndexOf(CONFIG_EXT, StringComparison.OrdinalIgnoreCase) ?? -1; // Get where the extension of the file path is in the str
    if (configExtPos >= 0) // Paranoia guard - it will exist because we checked before
    {
        var configFilePathLen = configExtPos + CONFIG_EXT.Length; // The length of the file path will be the pos of the ext + length of the ext
        var configFilePath = configExcMsgSubStrHasConfigExt?.Substring(0, configFilePathLen); // Get the file path.
        return configFilePath; // Return it.
    }
    }
    return null; // Didn't find the path.
}

}
© www.soinside.com 2019 - 2024. All rights reserved.