使用场景很多,只要是要创建文件就要。所以写了一个。以后到处用就是了。
/// <summary> /// 根据path递规创建文件夹 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static bool CreateDirectoryByPath(this string filePath) { var isOk = false; var filePathDirectory = filePath.Substring(0, filePath.LastIndexOf(@"\")); filePathDirectory = filePathDirectory.Replace("//", @"\").Replace("/", @"\"); var directory = filePathDirectory.Split('\\').ToList(); var existsDir = string.Empty; foreach (var item in directory) { existsDir += item + @"\"; if (!Directory.Exists(existsDir)) { Directory.CreateDirectory(existsDir); } } isOk = true; return isOk; }
调用
//导出成文件 var filePath = AppContext.BaseDirectory + @"WeldResultUpload\" + DateTime.Now.ToString("yyyyMMdd") + @"\" + Global.WorkpieceTrayCode + ".xlsx"; if (filePath.CreateDirectoryByPath()) { FileHelper.Export(weldResultList.ToDataTable(), filePath); //数据文件路径 Global.SecsServer.SetSvVal(10024, filePath); }
标签:filePathDirectory,filePath,C#,isOk,递规,existsDir,var,path From: https://www.cnblogs.com/stevenchen2016/p/17702334.html