//public bool SetApplicationInfo(ApplicationInfo appInfo) //{ // var currentAppEntity = FileProcessingHelper.GetFiles(appInfo.AppPath); // writeTxt("apppath:"+appInfo.AppPath+"1111"); // if (currentAppEntity != null && currentAppEntity.MDir != null) // { // currentAppEntity.AppName = appInfo.AppName; // currentAppEntity.AppVersion = appInfo.AppVersion; // if (AppList == null) // AppList = new List<ApplicationEntity>(); // AppList.Add(currentAppEntity); // return true; // } // return false; //} /// <summary> /// 下载文件 /// </summary> /// <param name="dlfile"></param> /// <returns></returns> public DlFileResult DownLoadFile(DlFile dlfile) { string path = FileProcessingHelper.GetUpLoadFilePath() + dlfile.ProjectName + "\\" + dlfile.FileName; DlFileResult file = new DlFileResult(); try { if (!File.Exists(path)) { var result = new DlFileResult(); result.Size = 0; result.IsSuccess = false; result.Message = ""; result.FileStream = new MemoryStream(); return(result); } file.FileStream = new MemoryStream(); Stream ms = new MemoryStream(); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.CopyTo(ms); ms.Position = 0; file.Size = ms.Length; file.FileStream = ms; file.IsSuccess = true; fs.Flush(); fs.Close(); } catch (Exception ex) { MyLog4NetInfo.ErrorInfo(string.Format("下载文件报错,文件名称:{0},错误消息:{1},错误堆栈{2},错误实例{3}", dlfile.FileName, ex.Message, ex.StackTrace, ex.InnerException)); throw; } return(file); }
public bool DeleteAppInfo(string appName) { MyLog4NetInfo.LogInfo("调用方法:DeleteAppInfo,准备删除项目:" + appName); if (AppList != null && AppList.Any() && AppList.Any(o => o.AppName == appName)) { using (var transactionScope = new TransactionScope()) { //删除内存中的数据 var effectCount = AppList.RemoveAll(o => o.AppName == appName); MyLog4NetInfo.LogInfo(string.Format("删除内存中项目:{0}的数据,删除{1}!", appName, effectCount > 0?"成功":"失败")); //删除info下的txt File.Delete(FileProcessingHelper.AppInfoXMLPath() + appName + ".txt"); MyLog4NetInfo.LogInfo(string.Format("删除项目:{0} 在AppInfo文件夹下的txt文件", appName)); //删除upload下的文件夹 var deleteResult = FileProcessingHelper.DeleteDir(FileProcessingHelper.GetUpLoadFilePath() + appName); MyLog4NetInfo.LogInfo(string.Format("删除项目:{0} 在UpLoadFile文件夹下的项目文件,删除{1}!", appName, deleteResult?"成功":"失败")); if (effectCount > 0) { transactionScope.Complete(); } MyLog4NetInfo.LogInfo(string.Format("调用方法:DeleteAppInfo,删除项目{0},最终删除结果:{1}", appName, effectCount > 0?"成功":"失败")); } } return(false); }
/// <summary> /// 删除指定目录的文件夹及文件夹下的文件 /// </summary> /// <param name="path"></param> /// <returns></returns> public static bool DeleteDir(string path) { if (Directory.Exists(path) == false) { return(false); } DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] files = dir.GetFiles(); try { foreach (var item in files) { File.Delete(item.FullName); } if (dir.GetDirectories().Length != 0) { foreach (var item in dir.GetDirectories()) { if (!item.ToString().Contains("$") && (!item.ToString().Contains("Boot"))) { DeleteDir(dir.ToString() + "\\" + item.ToString()); } } } Directory.Delete(path); return(true); } catch (Exception ex) { MyLog4NetInfo.ErrorInfo(string.Format("删除报错,当前删除路径{0},错误消息:{1},错误堆栈{2},错误实例{3}", path, ex.Message, ex.StackTrace, ex.InnerException)); return(false); } }
/// <summary> /// 记录日志 /// </summary> /// <param name="message">日志内容</param> /// <param name="isLog">true为日志,false为错误日志</param> public void WriteLog(string message, bool isLog) { if (isLog) { MyLog4NetInfo.LogInfo(message); } else { MyLog4NetInfo.ErrorInfo(message); } }
/// <summary> /// 将一个字符串反序列化为指定类型 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="xml">字符串</param> /// <returns></returns> public static T XMLDeserialize <T>(string xml) { T t = default(T); using (StringReader sdr = new StringReader(xml)) { try { XmlSerializer serializer = new XmlSerializer(typeof(T)); t = (T)serializer.Deserialize(sdr); } catch (Exception ex) { MyLog4NetInfo.LogInfo(string.Format("反序列化报错,错误XML:{0},错误信息:{1},错误堆栈:{2},错误实例:{3}", xml, ex.Message, ex.StackTrace, ex.InnerException)); } } return(t); }
/// <summary> /// 如果路径不存在则创建,存在返回true /// </summary> /// <param name="dirName"></param> /// <returns></returns> public bool DirIsExistOrCreate(string dirName, string projectName) { var currentPath = path + projectName + "\\" + dirName; if (!Directory.Exists(currentPath)) { try { Directory.CreateDirectory(currentPath); return(true); } catch (Exception ex) { MyLog4NetInfo.ErrorInfo(string.Format("创建文件夹:{0} 错误,错误信息:{1},错误堆栈:{2},错误实例:{3}", currentPath, ex.Message, ex.StackTrace, ex.InnerException)); return(false); } } return(true); }
/// <summary> /// 更新应用程序信息到XML中 /// </summary> /// <param name="appName"></param> public bool UpdateAppInfo(string appName) { var isTrue = false; var firstOrDefault = AppList.FirstOrDefault(o => o.AppName == appName); if (firstOrDefault != null) { try { FileProcessingHelper.XMLSerializer(firstOrDefault, AppDomain.CurrentDomain.BaseDirectory + "\\AppInfo\\" + appName + ".txt"); isTrue = true; } catch (Exception ex) { MyLog4NetInfo.ErrorInfo(string.Format("调用方法UpdateAppInfo报错,错误信息:{0},错误堆栈:{1},错误实例:{2}", ex.Message, ex.StackTrace, ex.InnerException)); writeTxt(ex.ToString()); } } return(isTrue); }
public HttpResponseMessage GetResumFile() { Count++; MyLog4NetInfo.LogInfo("the invoke times is:" + Count); //用于获取当前文件是否是续传。和续传的字节数开始点。 var md5str = HttpContext.Current.Request.QueryString["md5str"]; MyLog4NetInfo.LogInfo("receive params md5str is:" + md5str); var saveFilePath = HttpContext.Current.Server.MapPath("~/FilesDir/") + md5str; if (File.Exists(saveFilePath)) { var fs = File.OpenWrite(saveFilePath); var fslength = fs.Length.ToString(); fs.Close(); return(new HttpResponseMessage { Content = new StringContent(fslength, System.Text.Encoding.UTF8, "text/plain") }); } return(new HttpResponseMessage(HttpStatusCode.OK)); }
/// <summary> /// 上传文件 /// </summary> /// <param name="file"></param> /// <returns></returns> public UpFileResult UpLoadFile(UpFile file) { byte[] buffer = new byte[file.Size]; try { FileStream fs = new FileStream(path + file.ProjectName + file.FileName, FileMode.Create, FileAccess.Write); int count = 0; while ((count = file.FileStream.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, count); } fs.Flush(); fs.Close(); } catch (Exception ex) { MyLog4NetInfo.ErrorInfo(string.Format("上传文件出错,文件名:{0},错误消息:{1},错误堆栈:{2},错误实例:{3}", file.FileName, ex.Message, ex.StackTrace, ex.InnerException)); throw; } return(new UpFileResult(true, "")); }
/// <summary> /// 获取指定路径下的所有文件集合 /// </summary> /// <param name="path"></param> /// <returns></returns> public static ApplicationEntity GetFiles(string path) { MyLog4NetInfo.LogInfo("文件日志:" + path); return(CommonAction.GetFiles(path)); }
public void DownLoadBreak(string fileName) { MyLog4NetInfo.LogInfo("receive the paramas is :" + fileName); fileName = fileName.Substring(0, fileName.LastIndexOf('?')); fileName = HttpContext.Current.Server.MapPath("~/FilesDir/") + fileName; MyLog4NetInfo.LogInfo("after deal with the fileName is:" + fileName); HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"]; FileStream iStream = null; // Buffer to read 10K bytes in chunk: byte[] buffer = new Byte[10240]; // Length of the file: int length; // Total bytes to read: long dataToRead; try { string filename = Path.GetFileName(fileName); var etag = GetMD5HashFromFile(fileName); // Open the file. iStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read); context.Response.Clear(); // Total bytes to read: dataToRead = iStream.Length; MyLog4NetInfo.LogInfo("dataToRead is :" + dataToRead); long p = 0; context.Response.AddHeader("Accept-Ranges", "bytes"); MyLog4NetInfo.LogInfo("context.Request.Headers[\"Range\"] is " + context.Request.Headers["Range"]); if (context.Request.Headers["Range"] != null) { context.Response.StatusCode = 206; p = long.Parse(context.Request.Headers["Range"].Replace("bytes=", "").Replace("-", "")); } if (p != 0) { context.Response.AddHeader("Content-Range", "bytes " + p + "-" + ((long)(dataToRead - 1)) + "/" + dataToRead); } context.Response.Charset = ""; context.Response.AddHeader("Content-Length", ((long)(dataToRead - p)).ToString()); context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(context.Request.ContentEncoding.GetBytes(filename))); context.Response.AddHeader("ETag", etag); context.Response.Headers.Set("ETag", etag); iStream.Position = p; dataToRead = dataToRead - p; MyLog4NetInfo.LogInfo("iStream.Position=" + p + ";dataToRead=" + dataToRead); // Read the bytes. while (dataToRead > 0) { // Verify that the client is connected. if (context.Response.IsClientConnected) { // Read the data in buffer. length = iStream.Read(buffer, 0, 10240); // Write the data to the current output stream. context.Response.OutputStream.Write(buffer, 0, length); // Flush the data to the HTML output. context.Response.Flush(); buffer = new Byte[10240]; dataToRead = dataToRead - length; } else { //prevent infinite loop if user disconnects dataToRead = -1; } } } catch (Exception ex) { // Trap the error, if any. context.Response.Write("Error : " + ex.Message); MyLog4NetInfo.ErrorInfo(string.Format("下载文件出现了错误,错误信息:{0},错误堆栈:{1},错误实例:{2}", ex.Message, ex.StackTrace, ex.InnerException)); } finally { if (iStream != null) { //Close the file. iStream.Close(); } context.Response.End(); } }