/*
         * public string getMemoryUsedByID(string processId)
         * {
         * Connection wmiConnection = new Connection();
         * Win32_Process p = new Win32_Process(wmiConnection);
         * IList<Dictionary<string,string>> mUsed = p.GetPropertyValues();
         * //IDictionary<string, string> mUsed = p.GetPropertyValues();
         * string memUse = string.Empty;
         * foreach (Dictionary<string,string> processInfo in mUsed)
         * {
         *  if (processInfo.ContainsValue(processId))
         *  {
         *    processInfo.TryGetValue("PageFileUsage", out memUse);
         *  }
         *  if (memUse != null && memUse != string.Empty)
         *    return memUse;
         * }
         * return null;
         *
         * }
         *
         * public string getMemoryUsedByName(string processName, string parentProcessName)
         * {
         * return null;
         * }
         */

        public ProcessAndUsage getMemoryUsed(string mode)
        {
            ProcessAndUsage pau = new ProcessAndUsage();
            Connection      wmiConnection;

            if (mode == "sa")
            {
                //get local machine memory usage information.
                wmiConnection = new Connection();
                memUsagepList.setType("server");
                Dictionary <string, string> memoryUsed = getProcessMemoryUsage(wmiConnection);
                string value = string.Empty;
                memoryUsed.TryGetValue(memUsagepList.derby, out value);
                pau.ServerDerby = value;
                memoryUsed.TryGetValue(memUsagepList.jetty, out value);
                pau.ServerJetty = value;
                memoryUsed.TryGetValue(memUsagepList.deamon, out value);
                pau.ServerDeamon = value;
            }
            else
            {
                //get local machine memory usage information.
                wmiConnection = new Connection();
                memUsagepList.setType("client");
                Dictionary <string, string> ClientMemoryUsed = getProcessMemoryUsage(wmiConnection);
                string value = string.Empty;
                ClientMemoryUsed.TryGetValue(memUsagepList.jetty, out value);
                pau.ClientJetty = value;
                ClientMemoryUsed.TryGetValue(memUsagepList.deamon, out value);
                pau.ClientDeamon = value;

                //get remote machine memory usage information.
                wmiConnection = new Connection("CsDataManager10", "CsDataManager!0", "cnshw76k3w1x", "cnshw76k3w1x");
                memUsagepList.setType("server");
                Dictionary <string, string> ServerMemoryUsed = getProcessMemoryUsage(wmiConnection);
                ServerMemoryUsed.TryGetValue(memUsagepList.derby, out value);
                pau.ServerDerby = value;
                ServerMemoryUsed.TryGetValue(memUsagepList.jetty, out value);
                pau.ServerJetty = value;
                ServerMemoryUsed.TryGetValue(memUsagepList.deamon, out value);
                pau.ServerDeamon = value;
            }
            //mUsage.Add(memoryUsed);
            return(pau);
        }
        /*
        public string getMemoryUsedByID(string processId)
        {
          Connection wmiConnection = new Connection();
          Win32_Process p = new Win32_Process(wmiConnection);
          IList<Dictionary<string,string>> mUsed = p.GetPropertyValues();
          //IDictionary<string, string> mUsed = p.GetPropertyValues();
          string memUse = string.Empty;
          foreach (Dictionary<string,string> processInfo in mUsed)
          {
        if (processInfo.ContainsValue(processId))
        {
          processInfo.TryGetValue("PageFileUsage", out memUse);
        }
        if (memUse != null && memUse != string.Empty)
          return memUse;
          }
          return null;

        }

        public string getMemoryUsedByName(string processName, string parentProcessName)
        {
          return null;
        }
        */
        public ProcessAndUsage getMemoryUsed(string mode)
        {
            ProcessAndUsage pau = new ProcessAndUsage();
              Connection wmiConnection;
              if (mode == "sa")
              {
            //get local machine memory usage information.
            wmiConnection = new Connection();
            memUsagepList.setType("server");
            Dictionary<string, string> memoryUsed = getProcessMemoryUsage(wmiConnection);
            string value = string.Empty;
            memoryUsed.TryGetValue(memUsagepList.derby, out value);
            pau.ServerDerby = value;
            memoryUsed.TryGetValue(memUsagepList.jetty, out value);
            pau.ServerJetty = value;
            memoryUsed.TryGetValue(memUsagepList.deamon, out value);
            pau.ServerDeamon = value;
              }
              else
              {
            //get local machine memory usage information.
            wmiConnection = new Connection();
            memUsagepList.setType("client");
            Dictionary<string, string> ClientMemoryUsed = getProcessMemoryUsage(wmiConnection);
            string value = string.Empty;
            ClientMemoryUsed.TryGetValue(memUsagepList.jetty, out value);
            pau.ClientJetty = value;
            ClientMemoryUsed.TryGetValue(memUsagepList.deamon, out value);
            pau.ClientDeamon = value;

            //get remote machine memory usage information.
            wmiConnection = new Connection("CsDataManager10", "CsDataManager!0", "cnshw76k3w1x", "cnshw76k3w1x");
            memUsagepList.setType("server");
            Dictionary<string, string> ServerMemoryUsed = getProcessMemoryUsage(wmiConnection);
            ServerMemoryUsed.TryGetValue(memUsagepList.derby, out value);
            pau.ServerDerby = value;
            ServerMemoryUsed.TryGetValue(memUsagepList.jetty, out value);
            pau.ServerJetty = value;
            ServerMemoryUsed.TryGetValue(memUsagepList.deamon, out value);
            pau.ServerDeamon = value;
              }
              //mUsage.Add(memoryUsed);
              return pau;
        }