C# - ScheduledTasks类 - 如何指定localhost?

时间:2011-06-21 12:09:09

标签: c# .net windows

在尝试提取计划任务列表时,我遇到了定义本地计算机的问题。

“localhost”似乎不起作用,我怎么能定义当前/本地计算机

private void testing_Click(object sender, EventArgs e)
{
    // Get a ScheduledTasks object for the computer named "DALLAS"
    ScheduledTasks st = new ScheduledTasks(@"localhost");

    // Get an array of all the task names
    string[] taskNames = st.GetTaskNames();

    // Open each task, write a descriptive string to the console
    foreach (string name in taskNames)
    {
        Task t = st.OpenTask(name);
        MessageBox.Show("  " + t.ToString());
        t.Close();
    }

    // Dispose the ScheduledTasks object to release COM resources.
    st.Dispose();  
}

2 个答案:

答案 0 :(得分:3)

从文档中,计算机名称必须是UNC名称:

ScheduledTasks Constructor (String)

因此,以下内容应该有效:

string machineName = (@"\\" + System.Environment.MachineName);

ScheduledTasks st = new ScheduledTasks(machineName);

答案 1 :(得分:0)

尝试使用Environment.MachineName

ScheduledTasks st = new ScheduledTasks(Environment.MachineName);