windows C# - 如何使用 TaskSchedular 类列出特定用户的计划任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6477693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:11:49  来源:igfitidea点击:

C# - How to list Scheduled Tasks for a specific user with the TaskSchedular Class

c#.netwindows

提问by Mike

I was wondering if someone could help me, I am trying to list Scheduled Tasks from a specific user (Admin) on the local computer using the TaskScheduler Class (http://www.codeproject.com/KB/cs/tsnewlib.aspx) I have the following:

我想知道是否有人可以帮助我,我正在尝试使用 TaskScheduler 类 (http://www.codeproject.com/KB/cs/tsnewlib.aspx) 在本地计算机上列出来自特定用户(管理员)的计划任务我有以下几点:

    // richTextBox6.Text = string.Join(Environment.NewLine, taskNames);
    private void button22_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        string machineName = (@"\" + System.Environment.MachineName);
        ScheduledTasks st = new ScheduledTasks(machineName);
        // Get an array of all the task names
        string[] taskNames = st.GetTaskNames();
        foreach (var taskName in taskNames)
        {
            listBox1.Items.Add(taskName);
        }

        st.Dispose();
    }

"@"\"" Specifies that the local machine is the target, but is it possible to add a user to this? (Since it only lists tasks that are in C:\Windows\Tasks when executed)

"@"\"" 指定本地机器是目标,但是否可以向其中添加用户?(因为它只列出执行时 C:\Windows\Tasks 中的任务)

采纳答案by Frédéric Hamidi

There doesn't seem to be a way to specify a user name when performing the query (only a machine name), but you can filter the results yourself using the Creatorproperty:

似乎没有办法在执行查询时指定用户名(只有机器名),但您可以使用Creator属性自己过滤结果:

foreach (string taskName in st.GetTaskNames()) {
    using (Task task = st.OpenTask(taskName)) {
        if (task.Creator == "username") {
            listBox1.Items.Add(taskName);
        }
    }
}

回答by Luis Bonilla

This only works for tasks created and configured for Windows 2003, XP, Windows 2000.

这仅适用于为 Windows 2003、XP、Windows 2000 创建和配置的任务。

If you select for Windows 7, Windows 2008 or Windows Vista, Windows 2008 the jobs don't get stored with a .jobextension in the C:\Windows\Tasksdirectory. They get stored in C:\Windows\System32\Tasksdirectory with no file extension and in XML format.

如果您选择 Windows 7、Windows 2008 或 Windows Vista、Windows 2008,则作业不会以.job扩展名存储在C:\Windows\Tasks目录中。它们C:\Windows\System32\Tasks以 XML 格式存储在没有文件扩展名的目录中。

The DLL fails to retrieve those configured for Windows 7, Windows Vista, Windows 2008.

DLL 无法检索为 Windows 7、Windows Vista、Windows 2008 配置的那些。

回答by Prabhakantha

i doubt you can do this. windows tasks will be common for all the users please correct me if im wrong. but you can get the created user of the perticuler task or els you will be able to get the execution user of the particuler computer.

我怀疑你能做到这一点。Windows 任务对所有用户来说都是通用的,如果我错了,请纠正我。但是您可以获得 perticuler 任务的创建用户,或者 els 您将能够获得 particuler 计算机的执行用户。

but this will be differ when it comes to diffrent OS. such as windows 7 windows xp.

但是当涉及到不同的操作系统时,这会有所不同。如 windows 7 windows xp。