/// <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中追加是一样的 }