当前位置:编程学习 > C#/ASP.NET >>

加急!!!C#一, 如何获取VISTA管理员权限,以避免UAC弹出窗口;二,XP&vista下C#中如何创建、取消、删除、还原 系统还原点?

C#中: 

1.  本人在开发一个ASSEMBLY,这个ASSEMBLY是一个ActionEngine,在里面需要调用几个Action的Handler,但是在Vista下,(UAC)是需要一个权限的提升的(elevation),为了避免多次弹出提示窗口,我想让本ActionEngine的Assembly 获取管理员权限。但我不知道该如何实现?
  public ExecuteActionsResult ExecuteActions
         (
parameters...
         )
      {
         //parameters checking lines....


         TOKEN_ELEVATION_TYPE elevationType;
         elevationType = GetTokenElevationType();

         ExecuteActionsResult result = ExecuteActionsResult.None;

         bool isVistaOrHigher = false;
         bool isSecondEngineProcess = false;
         bool isElevated = false;
         bool processContinue = true;

         // OS version is vista or higher 
         if (Environment.OSVersion.Version.Major < 6)
         {
            isVistaOrHigher = true;
         }

         if (isVistaOrHigher && elevationType == TOKEN_ELEVATION_TYPE.TokenElevationTypeFull)
         {
            if (applicationNameCopy.StartsWith(secondActionsEngineInVista))
            {
               applicationNameCopy = applicationNameCopy.Substring(secondActionsEngineInVista.Length);
               isSecondEngineProcess = true;
            }
         }

         //check admin flag
            if (isVistaOrHigher)
            {
               elevationType = GetTokenElevationType();
               switch (elevationType)
               {
                  case TOKEN_ELEVATION_TYPE.TokenElevationTypeDefault:  //TokenElevationTypeDefault - User is not using a split token
                     isVistaOrHigher = false;
                     break;
                  case TOKEN_ELEVATION_TYPE.TokenElevationTypeFull:  //TokenElevationTypeFull - User has a split token, and the process is running elevated
                     if (isSecondEngineProcess)
                     {
                        isElevated = true;
                     }
                     else
                     {
                        isVistaOrHigher = false;
                     }
                     break;
          //TokenElevationTypeLimited - User has a split token, but the process is not running elevated
                  case TOKEN_ELEVATION_TYPE.TokenElevationTypeLimited: 
                     需要帮助的部分--开始-- 
                     该如何写代码创建第二个ActionEngine.ExecuteActions()?并获取管理员权限?
                     //ProcessStartInfo startInfo = new ProcessStartInfo();
                     //startInfo.UseShellExecute = true;
                     //startInfo.WorkingDirectory = Environment.CurrentDirectory;
                     //startInfo.FileName = Application.ExecutablePath;
                     //startInfo.Verb = "runas";
                     //try
                     //{
                     //   Process p = Process.Start(startInfo);
                     //}
                     //catch (System.ComponentModel.Win32Exception ex)
                     //{
                     //   return; //If cancelled, do nothing
                     //}
                     SolutionsEngine secondSolutionsEngine = new SolutionsEngine();
                     secondSolutionsEngine.ExecuteActions(rha, secondActionsEngineInVista + applicationNameCopy, device, solution);
            需要帮助的部分--结束--
                     break;
               }
            }
         }
         //继续执行各Action...
         if (!(isVista && isElevated )) processContinue =false;
         if (processContinue )
         {
           actionResult = ExecuteAction(rha, applicationName, device, solution, st as Action);
          OnActionsExecuted(aser);
         }
        // return result lines...
      }
 

2.  C#中如何创建、取消、删除、还原 系统的还原点?
下面是我做测试用的一个小东东,是可以创建系统还原点的,但是取消,删除和还原这些还原点却做不到,希望大侠帮一下忙。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Security.Cryptography;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using System.Runtime;
using System.Diagnostics;
using System.Security.Cryptography.Xml;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.XPath;
using System.Globalization;
using System.Management;
using System.Net;
using System.Runtime.InteropServices;

using Microsoft.Win32;

namespace First_XML_Parser
{
   public partial class Form1 : Form
   {
      [DllImport("srclient.dll")]
      private static extern bool SRSetRestorePoint(ref RESTOREPOINTINFO pInfo, ref SMGRSTATUS pStatus);
      [DllImport("srclient.dll")]
      private static extern bool SRRemoveRestorePoint(Int64 nStatus);


     internal enum TypeOfEvent
      {
         BEGIN_SYSTEM_CHANGE = 100,
         END_SYSTEM_CHANGE = 101,
         BEGIN_NESTED_SYSTEM_CHANGE = 102,
         END_NESTED_SYSTEM_CHANGE = 103,
      }

      internal enum RestorePtType
      {
         APPLICATION_INSTALL = 0,
         APPLICATION_UNINSTALL = 1,
         RESTORE = 6,
         CHECKPOINT = 7,
         DEVICE_DRIVER_INSTALL = 10,
         FIRSTRUN = 11,
         MODIFY_SETTINGS = 12,
         CANCELLED_OPERATION = 13,
         BACKUP_RECOVERY = 14,
         MAX_DESC = 64,
         MIN_EVENT = 100,
      }

      internal struct RESTOREPOINTINFO
      {
         internal TypeOfEvent dwEventType;
         internal RestorePtType dwRestorePtType;
         internal UInt64 llSequenceNumber;
         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
         internal string szDescription;
      }

      internal struct SMGRSTATUS
      {
         internal UInt64 llSequenceNumber;
         internal UInt32 nStatus;
      }
      
      //[DllImport("srclient.dll")]
      //public static extern bool SRSetRestorePoint(ref RESTOREPOINTINFO pInfo, ref SMGRSTATUS pStatus);


      public Form1()
      {
         InitializeComponent();
      }
      private void Form1_Load(object sender, EventArgs e)
      {
      }
      public static void MyClassInitialize()
      {

      }


      private void button1_Click(object sender, EventArgs e)
      {

         RESTOREPOINTINFO Rest;
         SMGRSTATUS Sm = new SMGRSTATUS();

         Sm.llSequenceNumber = 0;

         Rest.dwEventType = TypeOfEvent.BEGIN_SYSTEM_CHANGE;
         Rest.dwRestorePtType = RestorePtType.APPLICATION_INSTALL;
         Rest.szDescription = "my restore point";
         Rest.llSequenceNumber = 0;

         if (SRSetRestorePoint(ref Rest, ref  Sm)) //开始创建(可以成功的)
         {
            Rest.dwRestorePtType = RestorePtType.CANCELLED_OPERATION; //取消 (失败的,不知道怎么回事?照着C++的代码翻译过来的)
            Rest.dwEventType = TypeOfEvent.END_SYSTEM_CHANGE;
            Rest.llSequenceNumber = Sm.llSequenceNumber;
            SRSetRestorePoint(ref Rest, ref  Sm);
            //SRRemoveRestorePoint(Sm.llSequenceNumber ); //删除(好像不行,因为这个secquence number 是INT64,但这个函数却要求INT32的)
       //关于还原,就更不知道用哪个方法了。难道是来自srclient.dll的RestoreSnapShot()函数?
         }

         //这里是另外一种创建系统还原点的方法.,但仍然不知道如何取消,删除和还原.
         ManagementClass rptClass = new ManagementClass(new ManagementPath(@"\\.\root\default:SystemRestore"));

         object[] methodArgs = {  Sm.llSequenceNumber };
         //object[] methodArgs = { "HP Proactice Service Restore Point", 0, 100 };

         ////Execute the method
         //object result = rptClass.InvokeMethod("CreateRestorePoint", methodArgs);
         object result = rptClass.InvokeMethod("Restore", methodArgs);
      }
      private void button2_Click(object sender, EventArgs e)
      {
         Application.Exit();
      }
   }
}
 
--------------------编程问答-------------------- 还未用过Vista, 关注... --------------------编程问答-------------------- 和谐接分
[img=http://asp.daqing.net/baby/vote.asp?id=15][/img]
[img=http://asp.daqing.net/baby/vote.asp?id=20][/img] --------------------编程问答-------------------- 谢谢帮顶!
这两个问题我查遍了NNNNNNNNNN多网站和MSDN也查不到C#的代码。
希望高手帮忙解决!
--------------------编程问答-------------------- 涉及到新Windows系统的现成C#代码...

应该是比较少的...

^o^ --------------------编程问答-------------------- 确实如此,希望高手能进来帮忙解决一下问题。现在我尝试过的方法就差直接给微软工程师去邮件了 :(
顶上去,高手请赐教! --------------------编程问答-------------------- 这个...
除了关了UAC估计没别的办法.
vista下一个已经获得管理员权限的进程所创建的子进程将自动获得管理员权限.
LZ不妨从这方面下手.这样的话应该只用弹出一次UAC的窗口. --------------------编程问答-------------------- 谢谢楼上的,UAC估计不太可能都关掉的,因为我们没办法控制所有的CLIENT。
继续顶! --------------------编程问答-------------------- 我也想要知道呀 --------------------编程问答-------------------- 期待答案 !! --------------------编程问答-------------------- 应该是 在 控制台 呼叫一些程序,

或者 WMI 搞搞破坏

用 以什么身份来运行之类的

--------------------编程问答-------------------- 这个只有表示关注了。、、、 --------------------编程问答-------------------- 不可能来隐藏系统uac权限.无解!就是为了安全才设置这个的.
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,