如何使用c#创建还原点? [英] How to create restore point using c#?
本文介绍了如何使用c#创建还原点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好!
下面的代码返回true但它没有创建任何还原点。
Hello !
Below code returns true but it does not create any Restore Point.
public static bool CreateRestorePoint()
{
bool isCreated = true;
try
{
ManagementClass mcProcess = new ManagementClass
(
new ManagementScope("\\\\localhost\\root\\default"),
new ManagementPath(SYSTEM_RESTORE),
new ObjectGetOptions()
);
ManagementBaseObject mbObjectInput = mcProcess.GetMethodParameters(CREATE_SYSTEM_RESTORE_POINT);
mbObjectInput[SYSTEM_RESTORE_POINT_DESCRIPTION] = string.Format("Restore point created from C# at {0}", DateTime.Now);
mbObjectInput[SYSTEM_RESTORE_POINT_TYPE] = 0;
mbObjectInput[SYSTEM_RESTORE_EVENTTYPE] = 100;
ManagementBaseObject mbObjectOutput = mcProcess.InvokeMethod(CREATE_SYSTEM_RESTORE_POINT, mbObjectInput, null);
isCreated = (mbObjectInput == null) ? !isCreated : isCreated;
}
catch (ManagementException me)
{
//handle the error.
isCreated = !isCreated;
}
return isCreated;
}
推荐答案
我找到了一个更简单的解决方案,基于这个:http://alitarhini.wordpress.com/2010/10/31/programmatically-create-system-restore-point/ [ ^ ]
在c#中不是那么容易:(
代码只是很简单:
I have found a simpler solution, based on this one: http://alitarhini.wordpress.com/2010/10/31/programmatically-create-system-restore-point/[^]
It is not that easy in c# :(
The code is just as simple:
dynamic restPoint = Interaction.GetObject("winmgmts:\\\\.\\root\\default:Systemrestore");
if (restPoint != null)
{
if (restPoint.CreateRestorePoint("test restore point", 0, 100) == 0)
{
Console.WriteLine("Restore Point created successfully");
}
else
{
Console.WriteLine("Could not create restore point!");
}
}
但是,要编译这个,你需要添加对<$ c $的引用c> Microsoft.VisualBasic 和 Microsoft.CSharp
assamblies。请注意,您需要提升代码才能运行。
But, to have this compiled, you need to add reference to Microsoft.VisualBasic
and Microsoft.CSharp
assamblies. Please note, that you need elevated privileges for your code to run.
这篇关于如何使用c#创建还原点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文