標籤:
第一、開發原因:
要做一個前台顯示伺服器檔案的網頁,想用JS控制項做,資料格式使用JSON。所以就寫將某目錄轉化成JSON字串,但是突然覺得拼湊太囉嗦,而且定製化太高,不靈活,所以索性就寫將C#中的DirectoryInfo執行個體遞迴下去找到所有子檔案夾和檔案,然後輸出成JSON字串。
緊接著,發現很多C#資料模型都是一樣的結構,比如SHarepoint中的網站和子網站,每個網站有列表或文件庫。那何不擴充一下呢。
第二、基本原理:
使用Type.InvokeMember()執行執行個體的方法和取得屬性。
string path = @"E:\Work\test";DirectoryInfo folder = new DirectoryInfo(path);Type type = foldera.GetType();FileInfo[] files = (FileInfo[])type.InvokeMember("GetFiles", BindingFlags.Default | BindingFlags.InvokeMethod, null, folder, new object[] { });
String Name = (String)type.InvokeMember("Name", BindingFlags.Default | BindingFlags.GetProperty, null, folder, new object[] { });
第三、全部代碼:
using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Reflection;namespace GetFolderJsonData{ class Program { public static string[] ZIJINames = new string[] { "Folder" }; public static string[] FUSHUNames = new string[] { "Files" }; public static string[] ZIJIMethods = new string[] { "GetDirectories" }; public static string[] FUSHUMethods = new string[] { "GetFiles" }; public static string[] ZIJIFields = new string[] { "Name" }; public static string[] FUSHUFields = new string[] { "Name", "Extension" }; public static string resultJson = ""; static void Main(string[] args) { string path = @"E:\Work_28528033\test"; DirectoryInfo foldera = new DirectoryInfo(path); string dd = "GetFiles"; Type type = foldera.GetType(); FileInfo[] files = (FileInfo[])type.InvokeMember(dd, BindingFlags.Default | BindingFlags.InvokeMethod, null, foldera, new object[] { }); resultJson += "{"; GetJsonDataFromObject(foldera); resultJson += "}"; Write(); } public static void Write() { FileStream fs = new FileStream(@"E:\Work_28528033\result.txt", FileMode.Create); //獲得位元組數組 byte[] data = System.Text.Encoding.Default.GetBytes(resultJson); //開始寫入 fs.Write(data, 0, data.Length); //清空緩衝區、關閉流 fs.Flush(); fs.Close(); } //public static string GetJsonData(string path) //{ // string resultJson="[{"; // DirectoryInfo folder = new DirectoryInfo(path); // if (folder.GetDirectories().Length > 0) // { // resultJson += "Folder:"; // foreach (DirectoryInfo f in folder.GetDirectories()) // { // resultJson += ""; // GetJsonData(folder.FullName); // } // resultJson += ""; // } // if (folder.GetFiles().Length>0) // { // resultJson += ""; // foreach (FileInfo file in folder.GetFiles()) // { // resultJson += ""; // } // resultJson += ""; // } // return resultJson += "}]"; //} public static string GetJsonDataFromObject(object folder) { Type type = folder.GetType(); int diguiInt = -1; foreach (string ZIJIMethodsItem in ZIJIMethods) { diguiInt++; object[] folders = (object[])type.InvokeMember(ZIJIMethodsItem, BindingFlags.Default | BindingFlags.InvokeMethod, null, folder, new object[] { }); if (folders.Length>0) { resultJson += ZIJINames[diguiInt] + ":["; foreach (object dir in folders) { resultJson += "{"; Type diguiType = dir.GetType(); foreach (string ZIJINamesItem in ZIJIFields) { resultJson += ZIJINamesItem+":\""; resultJson += diguiType.InvokeMember(ZIJINamesItem, BindingFlags.Default | BindingFlags.GetProperty, null, dir, new object[] { }).ToString()+"\","; } GetJsonDataFromObject(dir); resultJson += "},"; } resultJson = resultJson.Substring(0, resultJson.Length - 1); resultJson += "],"; } } int FUSHUMethodsInt = -1; foreach (string FUSHUMethodsItem in FUSHUMethods) { FUSHUMethodsInt++; object[] files = (object[])type.InvokeMember(FUSHUMethodsItem, BindingFlags.Default | BindingFlags.InvokeMethod, null, folder, new object[] { }); if (files.Length>0) { resultJson +=FUSHUNames[FUSHUMethodsInt] + ":["; foreach (object fileItem in files) { resultJson += "{"; Type proType = fileItem.GetType(); foreach (string FUSHUFieldsItem in FUSHUFields) { resultJson += FUSHUFieldsItem + ":\""; resultJson += proType.InvokeMember(FUSHUFieldsItem, BindingFlags.Default | BindingFlags.GetProperty, null, fileItem, new object[] { }).ToString() + "\","; } resultJson = resultJson.Substring(0, resultJson.Length - 1); resultJson += "},"; } resultJson = resultJson.Substring(0, resultJson.Length - 1); resultJson += "]"; } } return resultJson; } }}
結束語:
http://www.bejson.com/ JSON格式化工具(隨便搜了一個,不一定是最好的)
使用:
ZIJI代表與與傳入類型相同。
FUSHU代表這種類型執行個體包含的另外一種類型的執行個體。
Field是欄位。
Name是顯示在JSON中的名字。Name與Method相對應。
如果還是看不懂那太正常了,自己做一個檔案夾試試吧。
附上測試檔案夾,工程代碼,得到結果:
{ Folder: [ { Name: "1.1F", Folder: [ { Name: "1.1.1F", Files: [ { Name: "1.1.1.1 (1).txt", Extension: ".txt" }, { Name: "1.1.1.1 (2).txt", Extension: ".txt" }, { Name: "1.1.1.1.txt", Extension: ".txt" } ] } ], Files: [ { Name: "1.1.1.txt", Extension: ".txt" }, { Name: "1.1.2.txt", Extension: ".txt" }, { Name: "1.1.3.txt", Extension: ".txt" } ] }, { Name: "1.2F", Files: [ { Name: "1.2.1.txt", Extension: ".txt" }, { Name: "1.2.2.txt", Extension: ".txt" }, { Name: "1.2.3.txt", Extension: ".txt" } ] } ], Files: [ { Name: "1.1.txt", Extension: ".txt" }, { Name: "1.2.txt", Extension: ".txt" }, { Name: "1.3.txt", Extension: ".txt" } ]}
http://files.cnblogs.com/files/yixiaozi/GetFolderJsonData.rar
http://files.cnblogs.com/files/yixiaozi/GetFolderJsonDatatestFolder.rar
【C#方法】將對象執行個體轉化為JSON字串