我需要一个例子,可以让我传递一个参数
例如执行 delete.exe/killme.txt
所以它会在重启后使用“MoveFile”删除killme.txt。
虽然请不要使用 MS 预编译版本,因为它有一个烦人的免责声明,每次它都在不同的计算机上运行。
最佳答案
您需要 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);
}
和一些示例代码:
if (!NativeMethods.MoveFileEx("a.txt", null, MoveFileFlags.DelayUntilReboot))
{
Console.Error.WriteLine("Unable to schedule 'a.txt' for deletion");
}
关于c# - C#中的"MoveFile"函数(重启后删除文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6077869/