我正在使用Task Scheduler Managed Wrapper from Codeplex。我面临一个奇怪的问题。我试图枚举多个远程服务器(V1和V2)中的任务,然后在数据网格中显示任务。当我连接到V1服务器(使用forceV1 = true)时会发生此问题。枚举工作正常 - 数据甚至传递到网格。但是当我将光标移到网格上时,我得到一个SecurityException,说'不允许请求的注册表访问'。有谁知道发生了什么事?
public partial class MainWindow : Window
{
public MainWindow()
{
String osVer;
using (var reg = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "deves07"))
using (var key = reg.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\"))
{
osVer = string.Format("Name:{0}, Version:{1}", key.GetValue("ProductName"), key.GetValue("CurrentVersion"));
}
TaskService ts;
if (osVer.Contains("2003"))
{
ts = new TaskService("REMOTE", "username", "domain", "password", true);
}
else
{
ts = new TaskService("REMOTE", "username", "domain", "password");
}
List<TaskInfo> taskList = new List<TaskInfo>();
EnumFolderTasks(ts.RootFolder, taskList);
DataContext = taskList;
}
void EnumFolderTasks(TaskFolder fld, List<TaskInfo> taskLst)
{
Debug.WriteLine(fld.Name);
foreach (Task task in fld.Tasks)
taskLst.Add(ActOnTask(task));
foreach (TaskFolder sfld in fld.SubFolders)
EnumFolderTasks(sfld, taskLst);
}
TaskInfo ActOnTask(Task t)
{
TaskInfo taskInfo = new TaskInfo();
taskInfo.Name = t.Name;
taskInfo.isRunning = t.IsActive;
taskInfo.NextRunTime = t.NextRunTime;
taskInfo.LastRunTime = t.LastRunTime;
//taskInfo.LastRunStatus = t.LastTaskResult;
return taskInfo;
}
}
public class TaskInfo
{
public string Name { set; get; }
public bool isRunning { set; get; }
public DateTime NextRunTime { set; get; }
public DateTime LastRunTime { set; get; }
public int LastRunStatus { set; get; }
}
答案 0 :(得分:0)
这是代码复合的dahall的礼貌(非常感谢)
首先,尝试将括号放在第一个
using
的范围内 声明。您也可以对TaskService
实例执行相同操作。 你可以这样做:List<TaskInfo> taskList = new List<TaskInfo>(); using (TaskService ts = new TaskService(REMOTE", "username", "domain", "password", osVer.Contains("2003"))) { EnumFolderTasks(ts.RootFolder, taskList); } DataContext = taskList;
我不确定为什么这只会发生在V1调用中,但这确实解决了我的问题。