Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EventLogSession无法正常工作_C#_.net_Event Log - Fatal编程技术网

C# EventLogSession无法正常工作

C# EventLogSession无法正常工作,c#,.net,event-log,C#,.net,Event Log,我使用EventLogSession、EventLogQuery和EventLogReader类来查询远程计算机,当我使用以下代码时,它工作正常: var session = new EventLogSession(machineName, null, null, null, SessionAuthentication.Default); 但在我指定域、用

我使用EventLogSession、EventLogQuery和EventLogReader类来查询远程计算机,当我使用以下代码时,它工作正常:

var session = new EventLogSession(machineName, 
                                  null, null, null, 
                                  SessionAuthentication.Default);
但在我指定域、用户名、密码和我的本地机器完全相同之后,它就不工作了,抛出

“试图执行未经授权的操作”(UnauthorizedAccessException)异常

我使用的代码如下所示:

var password = new SecureString();
passwordString.ToCharArray().ForEach(ch => password.AppendChar(ch));
var session = new EventLogSession(machineName, 
                                  "domain", "username", password, 
                                   SessionAuthentication.Default);
MSDN表示,当域、用户名和密码都为空时,EventLogSession将使用本地计算机的凭据。但当我在代码中指定它们时,它不起作用,怎么会这样呢

顺便说一句,我正在使用WindowsServer2008R2测试代码。但我怀疑这是否依赖于操作系统

更新:原来这是因为我的愚蠢,因为我忘记了LINQ的懒惰,或者更确切地说是C#的收益。为方便起见,我将ForEach实现为以下扩展方法

public static IEnumerable<T> ForEach<T>(this IEnumerable<T> sequence, Action<T> action)
{
    foreach (var item in sequence)
    {
        action(item);
        yield return item;
    }
}
公共静态IEnumerable ForEach(此IEnumerable序列,动作)
{
foreach(序列中的var项目)
{
行动(项目);
收益回报项目;
}
}
这样,ForEach在ForEach之前不会实际执行动作委托。因此,我从不在char序列上调用foreach,它将永远不会执行,因此密码根本没有初始化

抱歉打扰你们了,我把塔米的回答标为接受。

public void QueryRemoteComputer()
    {
        string queryString = "*[System/Level=2]"; // XPATH Query
        SecureString pw = GetPassword();

        EventLogSession session = new EventLogSession(
            "RemoteComputerName",                               // Remote Computer
            "Domain",                                  // Domain
            "Username",                                // Username
            pw,
            SessionAuthentication.Default);

        pw.Dispose();

        // Query the Application log on the remote computer.
        EventLogQuery query = new EventLogQuery("Application", PathType.LogName, queryString);
        query.Session = session;

        try
        {
            EventLogReader logReader = new EventLogReader(query);

            // Display event info
            DisplayEventAndLogInformation(logReader);
        }
        catch (EventLogException e)
        {
            Console.WriteLine("Could not query the remote computer! " + e.Message);
            return;
        }
    }