檔案格式為

[業務部]

IP=10.8.210.66

IP=10.8.210.67

 

[行政部]

IP=192.168.12.11

IP=192.168.12.12

 

[財務部]

IP=192.168.12.13

IP=192.168.12.14

=======================================================================================================

以下程式為自訂函式 public static Hashtable BuildSortList() 執行完畢會回傳一個

Hashtable,裡面會放N個 List,N=分類檔的部門數量

每個List 裡會放N個IP,N=部門底下的IP數量

=======================================================================================================

        //讀取分類檔
        //執行完畢會回傳一個 Hashtable,裡面會放N個 List,N=分類檔的部門數量
        //每個List 裡會放N個IP,N=部門底下的IP數量
        public static Hashtable BuildSortList()
        {
            //部門名稱清單
            SortStruct ss = new SortStruct();
            ss.sortNameList = new List();
            
            ipss.IPtoSortHT = new Hashtable();
            List sort = new List();
            //用來放sort List            
            Hashtable ht = new Hashtable();

            String strLine;
            //分類數量
            int sortCount = 0;

            string strName;
            //群組名稱陣列
            string[] sortName;

            //開啟檔案  C:\
            StreamReader sr = new StreamReader(@"D:\\DVM2\\Source\\DVM2SingleReport\\CrystalReportsApplicationTest\\bin\Report\\SortList.txt");

            //判斷有幾個分類
            StreamReader sr2 = new StreamReader(@"D:\\DVM2\\Source\\DVM2SingleReport\\CrystalReportsApplicationTest\\bin\Report\\SortList.txt");
            //幾號部門
            int sortNumber = -1;
            int sortNumber2 = -1;

            //讀取到一次"[",sortCount就+1
            while ((strLine = sr2.ReadLine()) != null)
            {
                strName = "";
                int iTitle = -1;
                iTitle = strLine.IndexOf("[", StringComparison.CurrentCultureIgnoreCase);
                if (iTitle >= 0)
                {
                    sortCount++;
                    //讀取到"["開頭時將兩邊做切割,取中間文字
                    sortName = strLine.Split(new char[2] {'[', ']'});
                    foreach(string s in sortName)
                    {
                        strName += s;
                    }
                    //將部門名稱放入List
                    ss.sortNameList.Add(strName);
                }
            }
            //部門數量 > 0 才開始讀檔
            if (sortCount > 0)
            {
                //讀一行
                while ((strLine = sr.ReadLine()) != null)
                {
                    //所在位置
                    int iPos = strLine.IndexOf("=");

                    //string[] strLines = strLine.Split('=');

                    string strKey = string.Empty;

                    string strValue = string.Empty;

                    // 如果有出現 "=" 才可以切割欄位
                    if (iPos > 0)
                    {
                        strKey = strLine.Substring(0, iPos).Trim();
                        strValue = strLine.Substring(iPos + 1).Trim();
                    }

                    //判斷目前這行是什麼部門
                    for (int i = 0; i < sortCount; i++)
                    {
                        int iTitle = strLine.IndexOf("[", StringComparison.CurrentCultureIgnoreCase);
                        //判斷是否讀取到新的部門
                        if (iTitle > -1)
                        {
                            sortNumber2++;
                            if (sortNumber2 == 0) break;
                        }
                        if (sortNumber2 > 0 && iTitle > -1)
                        {
                            sortNumber = i;
                            //將上一個讀取完的部門儲存
                            if (sortNumber >= 0) ht.Add(ss.sortNameList[sortNumber2-1], sort);
                            //if (sortNumber > 0) ht.Add("sort" + i, sort);
                            //讀取新的部門就重新new一個List                            
                            sort = new List();
                            break;
                        }
                    }

                    //將讀取到的IP或電腦名稱加入List
                    switch (strKey)
                    {
                        case "IP":
                            sort.Add(strValue);
                            ipss.IPtoSortHT.Add(strValue.ToString().Trim(), ss.sortNameList[sortNumber2]);
                            break;
                        case "Host":
                            sort.Add(strValue);
                            break;
                    }
                }
                //如果只有一個部門
                if (sortCount == 1)
                {
                    ht.Add(ss.sortNameList[0], sort);
                    return ht;
                }
            }
            //將最後一個部門加入List
            ht.Add(ss.sortNameList[sortCount-1], sort);
            return ht;
        }

=======================================================================================================

 

文章標籤
全站熱搜
創作者介紹
創作者 JoyceHsu1126 的頭像
JoyceHsu1126

Joyce Hsu 的部落格

JoyceHsu1126 發表在 痞客邦 留言(0) 人氣(10)