windows C#中的“MoveFile”函数(重启后删除文件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6077869/
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 16:53:26  来源:igfitidea点击:

"MoveFile" function in C# (Delete file after reboot)

c#.netwindows

提问by Jay

I am in need of an example, that can let me pass a parameter

我需要一个例子,它可以让我传递一个参数

e.g. executing delete.exe /killme.txt

例如执行delete.exe /killme.txt

So it will use the "MoveFile" to delete killme.txt after reboot.

所以它会在重启后使用“MoveFile”删除killme.txt。

Although please not the MS precompiled version, as it has an annoying disclaimer, every time its run on a different computer.

虽然请不要使用 MS 预编译版本,因为它有一个烦人的免责声明,每次在不同的计算机上运行。

回答by user7116

You'll need the P/Invoke declarations for MoveFileEx:

您将需要MoveFileEx的 P/Invoke 声明:

[Flags]
internal enum MoveFileFlags
{
    None = 0,
    ReplaceExisting = 1,
    CopyAllowed = 2,
    DelayUntilReboot = 4,
    WriteThrough = 8,
    CreateHardlink = 16,
    FailIfNotTrackable = 32,
}

internal static class NativeMethods
{
    [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
    public static extern bool MoveFileEx(
        string lpExistingFileName,
        string lpNewFileName, 
        MoveFileFlags dwFlags);
}

And some example code:

和一些示例代码:

if (!NativeMethods.MoveFileEx("a.txt", null, MoveFileFlags.DelayUntilReboot))
{
    Console.Error.WriteLine("Unable to schedule 'a.txt' for deletion");
}

回答by David Anderson

Because you want to perform this after reboot as a requirement, you could use the Windows Task Scheduler API. You can invoke this in C# by adding a reference to the COM library TaskScheduler 1.1 Type Library. Below is a full code example on running Notepad.exe at logon.

因为您希望在重新启动后作为要求执行此操作,所以您可以使用 Windows 任务计划程序 API。您可以在 C# 中通过添加对 COM 库 TaskScheduler 1.1 类型库的引用来调用它。下面是在登录时运行 Notepad.exe 的完整代码示例。

Also, here is another resource: http://bartdesmet.net/blogs/bart/archive/2008/02/23/calling-the-task-scheduler-in-windows-vista-and-windows-server-2008-from-managed-code.aspx

另外,这里是另一个资源:http: //bartdesmet.net/blogs/bart/archive/2008/02/23/calling-the-task-scheduler-in-windows-vista-and-windows-server-2008-from -managed-code.aspx

You could call the system command DEL from Windows Command line, potentially with this code.

您可以从 Windows 命令行调用系统命令 DEL,可能使用此代码。

namespace TaskSchedulerExample {
    using System;
    using TaskScheduler;

    class Program {
        static void Main(string[] args) {
            var scheduler = new TaskSchedulerClass();
            scheduler.Connect(null, null, null, null);

            ITaskDefinition task = scheduler.NewTask(0);
            task.RegistrationInfo.Author = "DCOM Productions";
            task.RegistrationInfo.Description = "Demo";

            ILogonTrigger trigger = (ILogonTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
            trigger.Id = "Logon Demo";

            IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
            action.Id = "Delete";
            action.Path = "c:\delete.exe";          // Or similar path
            action.WorkingDirectory = "c:\";        // Working path
            action.Arguments = "c:\killme.txt";     // Path to your file

            ITaskFolder root = scheduler.GetFolder("\");
            IRegisteredTask regTask = root.RegisterTaskDefinition("Demo", task, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");

            //Force run task
            //IRunningTask runTask = regTask.Run(null);
        }
    }
}

This gives you some flexibility. You could run your own delete.exe, or you could potentially invoke the Windows Command Line to execute the DEL command.

这为您提供了一些灵活性。您可以运行自己的 delete.exe,也可以潜在地调用 Windows 命令行来执行 DEL 命令。