Esempio n. 1
0
        /// <summary>
        /// the fileItem.DestinationFullName is like this \\computername\c$\aaa\xx.dll
        /// </summary>
        /// <param name="fileItem"></param>
        public static void TakeOwnership(FileItem fileItem)
        {
            string msg = string.Format("taking ownership of file begins: destination: {0} ", fileItem.DestinationFullName);
            Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg);

            string localFormatPath = @"C:" + fileItem.DestinationFullName.Substring(fileItem.DestinationFullName.IndexOf("c$", StringComparison.OrdinalIgnoreCase) + 2);

            string objPath = string.Format("CIM_DataFile.Name='{0}'", localFormatPath);

            ConnectionOptions connectionOptions = new ConnectionOptions();
            connectionOptions.Authentication = AuthenticationLevel.Packet;
            connectionOptions.EnablePrivileges = true;
            connectionOptions.Username = Singleton<Constants>.UniqueInstance.UserName;
            connectionOptions.Password = Singleton<Constants>.UniqueInstance.PassWord;
            connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
            try
            {
                ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", Singleton<Constants>.UniqueInstance.MachineName), connectionOptions);
                scope.Connect();
                using (ManagementObject serviceObj = new ManagementObject(scope, new ManagementPath(objPath), null))
                {

                    ManagementBaseObject outParams = serviceObj.InvokeMethod("TakeOwnerShip", null, null);
                    uint returnValue = (uint)outParams["ReturnValue"];

                    msg = string.Format("take ownership for file: {0} result: {1}", localFormatPath, returnValue);
                    Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg);

                    ManagementBaseObject inParams = serviceObj.GetMethodParameters("ChangeSecurityPermissions");

                    inParams["Option"] = 4;
                    inParams["SecurityDescriptor"] = EveryOneSecDescriptor;

                    ManagementBaseObject outParamsOfChangingPermission = serviceObj.InvokeMethod("ChangeSecurityPermissions", inParams, null);
                    uint returnValueOfChangingPermission = (uint)outParamsOfChangingPermission["ReturnValue"];

                    msg = string.Format("changing permission for file: {0} result: {1}", localFormatPath, returnValueOfChangingPermission);
                    Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg);
                }
            }
            catch (Exception ex)
            {
                msg = string.Format("could not take ownership of file {0} , {1}", localFormatPath, ex);
                Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg, LogLevel.Warning);
            }
            msg = string.Format("taking ownership of file ends: destination: {0}", localFormatPath);
            Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg);
        }
Esempio n. 2
0
        public static void ReplaceFile(FileItem fileItem)
        {
            string msg = string.Format("replacing file begins: source: {0} destination: {1}", fileItem.SourceFullName, fileItem.DestinationFullName);
            Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg);
            try
            {
                if (!File.Exists(fileItem.DestinationFullName + ".bak"))
                {
                    File.Copy(fileItem.DestinationFullName, fileItem.DestinationFullName + ".bak");
                }
                File.Copy(fileItem.SourceFullName, fileItem.DestinationFullName, true);
            }
            catch (Exception e)
            {
                msg = string.Format("exception encounterred  when replacing file: {0}, exception:{1}", fileItem.DestinationFullName, e);
                Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg);
            }

            msg = string.Format("replacing file ends: source: {0} destination: {1}", fileItem.SourceFullName, fileItem.DestinationFullName);
            Singleton<ReportMediator>.UniqueInstance.ReportStatus(msg);
        }