コード例 #1
0
        private void buttonImportCatalog_Click(object sender, EventArgs e)
        {
            // 把内容写入到数据库
            if (treeViewCatalog.Nodes.Count == 0)
            {
                return;
            }

            try
            {
                UserCatalog      config    = new UserCatalog();
                Queue <TreeNode> nodesList = new Queue <TreeNode>();
                nodesList.Enqueue(treeViewCatalog.Nodes[0]);

                while (nodesList.Count > 0)
                {
                    TreeNode currentNode = nodesList.Dequeue();
                    currentNode.Tag = currentNode.Text;
                    if (currentNode.Parent != null)
                    {
                        currentNode.Tag = currentNode.Parent.Tag as string + "." + currentNode.Text;
                    }

                    string[] nodeValue = currentNode.Text.Split(':');
                    if (nodeValue.Length == 3)
                    {
                        string strKeyValue = currentNode.Parent.Tag as string;
                        if (!config.catalogs.ContainsKey(strKeyValue))
                        {
                            config.catalogs[strKeyValue] = new List <CatalogItem>();
                        }

                        config.catalogs[strKeyValue].Add(
                            new CatalogItem
                        {
                            name  = nodeValue[0],
                            value = nodeValue[1],
                            index = Convert.ToInt32(nodeValue[2])
                        });
                    }
                    else
                    {
                        config.catalogs.Add(currentNode.Tag as string, new List <CatalogItem>());
                    }

                    foreach (TreeNode v in currentNode.Nodes)
                    {
                        nodesList.Enqueue(v);
                    }
                }

                StringWriter sw     = new StringWriter();
                JsonWriter   writer = new JsonTextWriter(sw);
                writer.WriteStartObject();

                foreach (var item in config.catalogs)
                {
                    writer.WritePropertyName(StringToUnicode(item.Key));
                    writer.WriteStartArray();
                    if (item.Value.Count != 0)
                    {
                        foreach (var childItem in item.Value)
                        {
                            writer.WriteStartArray();
                            writer.WriteValue(StringToUnicode(childItem.name));
                            writer.WriteValue(childItem.value);
                            writer.WriteValue(childItem.index);
                            writer.WriteEndArray();
                        }
                    }
                    writer.WriteEndArray();
                }
                writer.WriteEndObject();
                writer.Flush();

                string strJson = sw.ToString();
                if (!string.IsNullOrEmpty(strJson))
                {
                    strJson = strJson.Replace("\\\\", "\\");
                    string        sql     = "update preferences set val='{0}' where key='user_categories'";
                    SQLiteCommand command = new SQLiteCommand(string.Format(sql, strJson), m_dbConnection);
                    command.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
            }
        }
コード例 #2
0
        private void buttonRefreshCatalog_Click(object sender, EventArgs e)
        {
            try
            {
                string           sql     = "select val from preferences where key='user_categories'";
                SQLiteCommand    command = new SQLiteCommand(sql, m_dbConnection);
                SQLiteDataReader reader  = command.ExecuteReader();
                if (reader.Read())
                {
                    string  value = reader["val"] as string;
                    JObject jo    = JObject.Parse(value);
                    if (jo.Count > 0)
                    {
                        UserCatalog userCatalog = new UserCatalog();
                        foreach (var child in jo)
                        {
                            List <CatalogItem> items     = new List <CatalogItem>();
                            JArray             childObjs = JArray.Parse(child.Value.ToString());
                            if (childObjs.Count > 0)
                            {
                                foreach (var item in childObjs)
                                {
                                    items.Add(new CatalogItem
                                    {
                                        name  = item[0].ToString(),
                                        value = item[1].ToString(),
                                        index = Convert.ToInt32(item[2].ToString())
                                    }
                                              );
                                }
                            }

                            userCatalog.catalogs.Add(child.Key, items);
                        }

                        // 显示树
                        treeViewCatalog.Nodes.Clear();
                        foreach (var node in userCatalog.catalogs)
                        {
                            TreeNode           CurrentNode  = null;
                            TreeNodeCollection CurrentNodes = treeViewCatalog.Nodes;
                            string[]           nodeName     = node.Key.Split('.');
                            foreach (string name in nodeName)
                            {
                                if (!CurrentNodes.ContainsKey(name))
                                {
                                    CurrentNode = CurrentNodes.Add(name, name);
                                }
                                else
                                {
                                    CurrentNode = CurrentNodes[name];
                                }

                                CurrentNodes = CurrentNode.Nodes;
                            }

                            if (CurrentNode != null)
                            {
                                foreach (CatalogItem leaf in node.Value)
                                {
                                    CurrentNode.Nodes.Add(leaf.name + ":" + leaf.value + ":" + leaf.index);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }