/// <summary> /// 删除文件 /// </summary> /// <param name="path">路径</param> public static void DeleteFile(string path) { if (SysFile2.Exists(path)) { File.Delete(path); } }
/// <summary> /// 读取文件内所有字串 /// </summary> /// <param name="fileName">文件名/包括路径</param> /// <returns>文件内容</returns> public static string ReadFile(string fileName) { try { SysFile2.SetNormal(fileName); FileStream fs = new FileStream(fileName, FileMode.Open); StreamReader sr = new StreamReader(fs, Encoding.Default); string file = sr.ReadToEnd(); sr.Close(); fs.Close(); return(file); } catch (Exception ex) { throw new Exception("读取文件内容异常![" + ex.Message + "]"); } }
/// <summary> /// 写Framework日志文件 /// </summary> public static void WriteLog(string logStr) { try { string logpath = SysConfig.LogFile; if (SysFile2.Exists(logpath)) { FileInfo fi = new FileInfo(logpath); long mn = fi.Length; if (mn > SysConfig.LogFileLength)//大于则删除文件 { fi.Delete(); } } StreamWriter sw = new StreamWriter(logpath, true, Encoding.Unicode); sw.WriteLine(DateTime.Now.ToString("yy/MM/dd HH:mm:ss") + " " + logStr); sw.Close(); } catch { } }
/// <summary> /// 文件下载 /// </summary> /// <param name="downUrl">下载地址</param> /// <param name="downFile">要下载的文件</param> /// <returns></returns> public static bool DownFile(string downUrl, string downFile) { long fileLength = 0; WebRequest webReq = WebRequest.Create(downUrl); WebResponse webRes = webReq.GetResponse(); fileLength = webRes.ContentLength; try { Stream srm = webRes.GetResponseStream(); StreamReader srmReader = new StreamReader(srm); byte[] bufferbyte = new byte[fileLength]; int allByte = (int)bufferbyte.Length; int startByte = 0; while (fileLength > 0) { int downByte = srm.Read(bufferbyte, startByte, allByte); if (downByte == 0) { break; } startByte += downByte; fileLength -= downByte; } SysFile2.CreateDirtory(downFile); FileStream fs = new FileStream(downFile, FileMode.Create, FileAccess.Write); fs.Write(bufferbyte, 0, startByte); srm.Close(); srmReader.Close(); fs.Close(); return(true); } catch { return(false); } }