/// <summary> /// 將json寫入js文件 /// 用法:先獲取一個集合,序列化后 執行WriteToJs /// JavaScriptSerializer js = new JavaScriptSerializer(); /// string Workers_Json = js.Serialize(Workers_Obj); /// </summary> /// <param name="jsonStr">json字符串</param> /// <param name="jsName">js文件名</param> public static void WriteToJs(string jsonStr, string jsName) { try { string url = string.Format("/Content/js/sys_{0}.js", jsName); string path = HttpContext.Current.Server.MapPath(url); FileInfo info = new FileInfo(path); StreamWriter sr = new StreamWriter(path, false, Encoding.GetEncoding("utf-8")); if (!File.Exists(path))//如果不存在就創建 { sr = File.CreateText(path); } //if (info.Length != 0)//如果為空就寫 //{ // sr = new StreamWriter(path, false, Encoding.GetEncoding("utf-8")); //} sr.Write(string.Format("var sys_{0}={1}", jsName, content)); sr.Close(); sr.Dispose(); } catch (Exception ex) { /**/ throw new Exception(ex.Message); } //追加內容和向txt中追加是一樣的 }