/// <summary>
        /// Write custom property e.g.: ListName, ValueColumn, ParentLookup, ExpandCollapse, KeyColumn.
        /// </summary>
        private void WriteCustomProperties()
        {
            this.SetCustomProperty(GlobalConstants.TREEVIEW_PROPERTY_LISTNAME, this.ListName);
            this.SetCustomProperty(GlobalConstants.TREEVIEW_PROPERTY_VALUECOLUMN, this.ValueColumn);
            this.SetCustomProperty(GlobalConstants.TREEVIEW_PROPERTY_PARENTLOOKUP, this.ParentLookup);
            this.SetCustomProperty(GlobalConstants.TREEVIEW_PROPERTY_EXPANDCOLLAPSE, this.ExpandCollapse);
            this.SetCustomProperty(GlobalConstants.TREEVIEW_PROPERTY_KEYCOLUMN, this.KeyColumn);

            //Clear the new column property flags
            lock (_NewColumnProperties)
            {
                string contextKey = string.Empty;
                contextKey = _ContextID + GlobalConstants.TREEVIEW_PROPERTY_LISTNAME;
                if (_NewColumnProperties.ContainsKey(contextKey))
                {
                    _NewColumnProperties.Remove(contextKey);
                }

                contextKey = _ContextID + GlobalConstants.TREEVIEW_PROPERTY_VALUECOLUMN;
                if (_NewColumnProperties.ContainsKey(contextKey))
                {
                    _NewColumnProperties.Remove(contextKey);
                }

                contextKey = _ContextID + GlobalConstants.TREEVIEW_PROPERTY_PARENTLOOKUP;
                if (_NewColumnProperties.ContainsKey(contextKey))
                {
                    _NewColumnProperties.Remove(contextKey);
                }

                contextKey = _ContextID + GlobalConstants.TREEVIEW_PROPERTY_EXPANDCOLLAPSE;
                if (_NewColumnProperties.ContainsKey(contextKey))
                {
                    _NewColumnProperties.Remove(contextKey);
                }

                contextKey = _ContextID + GlobalConstants.TREEVIEW_PROPERTY_KEYCOLUMN;
                if (_NewColumnProperties.ContainsKey(contextKey))
                {
                    _NewColumnProperties.Remove(contextKey);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// run a command under the specify user's account.  Specify user to 'null'
        /// to run under the current authentication.  Also support for setting the
        /// DOS environment variables.
        /// </summary>
        /// <param name="command"></param>
        /// <param name="dosEnv"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public void Run(string command, Dictionary <String, String> dosEnv, UserAccount user)
        {
            log = new List <string>();

            try
            {
                using (Process p = new Process())
                {
                    // create a batch file ...
                    string batchFile = FileUtil.CreateTempFile("bat", new string[] { command });


                    // setup the command-lines ...
                    p.StartInfo.FileName = Environment.GetEnvironmentVariable("COMSPEC");
                    // use two doublequote to support spaces in filename/folder.
                    p.StartInfo.Arguments = String.Format("/c \"\"{0}\"\"", batchFile);
                    LogMessage("Filename/Arguments: " + p.StartInfo.FileName + " " + p.StartInfo.Arguments, false);

                    // runas user ...
                    {
                        string username;

                        if (user == null)
                        {
                            // use current auth, nothing to do!
                            username = String.Format("{1}\\{0}", Environment.UserName, Environment.UserDomainName);
                        }
                        else
                        {
                            p.StartInfo.UserName = user.Username;
                            p.StartInfo.Password = user.Password;
                            p.StartInfo.Domain   = user.Domain;
                            username             = user.Fullname;
                            //user.Password.Copy();
                        }
                        LogMessage("Run as : " + username, false);
                    }

                    // enable standard i/o/e redirection ...
                    if (enableStandardRedirection)
                    {
                        LogMessage("Enabling standard output/error redirection.", false);
                        p.StartInfo.UseShellExecute        = false;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.StartInfo.RedirectStandardError  = true;
                        p.StartInfo.RedirectStandardInput  = true;

                        // do not show dos prompt ...
                        p.StartInfo.CreateNoWindow = true;
                    }

                    // set environment variables ...
                    if ((dosEnv != null) && (dosEnv.Count > 0))
                    {
                        System.Collections.Specialized.StringDictionary var = p.StartInfo.EnvironmentVariables;

                        foreach (KeyValuePair <String, String> kv in dosEnv)
                        {
                            if (var.ContainsKey(kv.Key))
                            {
                                var.Remove(kv.Key);
                            }

                            var.Add(kv.Key, kv.Value);
                        }
                    }

                    // start the process here ...
                    LogMessage("Executing command: " + command, false);
                    LogMessage("");
                    p.Start();

                    // handle standard i/o/e redirection ...
                    if (enableStandardRedirection)
                    {
                        // force Ctrl-Z to standard input, just in case the application prompts
                        // for input.  This will prevent an application from hanging.
                        p.StandardInput.Close();

                        ProcessRedirectStream(p.StandardOutput, "Standard Output");
                        ProcessRedirectStream(p.StandardError, "Standard Error");
                    }

                    // wait for the specify amount of time ...
                    if (ExecutionWaitAmount > 0)
                    {
                        // converts seconds --> milliseconds.
                        p.WaitForExit(ExecutionWaitAmount * 1000);
                    }

                    // check exit status ...
                    if (p.HasExited)
                    {
                        LogMessage("Application closed gracefully.");
                    }
                    else
                    {
                        #region Perform extra precautions
                        if (!p.HasExited)
                        {
                            LogMessage("Sending the close.");
                            p.CloseMainWindow();
                            Thread.Sleep(500);
                        }

                        if (!p.HasExited)
                        {
                            LogMessage("NOTE: Application not responding.  Sending the kill.");
                            p.Kill();
                            Thread.Sleep(500);
                        }

                        if (!p.HasExited)
                        {
                            LogMessage("WARNING: Unable to kill application.  (Application must be closed manually!)");
                        }
                        #endregion
                    }
                    LogMessage("");

                    ExitCode = p.ExitCode;

                    LogProcessInfo(p);

                    //return log.ToArray();
                }
            }
            catch (Exception ex)
            {
                LogMessage("Error: " + ex.ToString());
                throw;
            }
        }