123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- namespace WWPipeLine.Commons
- {
- public class Paths
- {
- private static string ApplicationExePath
- {
- get
- {
- string codeBase = Assembly.GetExecutingAssembly().Location;
- UriBuilder uri = new UriBuilder(codeBase);
- string path = Uri.UnescapeDataString(uri.Path);
- return Path.GetDirectoryName(path);
- }
- }
- public static string ApplicationPath
- {
- get
- {
- FileInfo f = new FileInfo(ApplicationExePath);
- return f.Directory.FullName;
- }
- }
-
- public static string ApplicationConfigDir
- {
- get
- {
- FileInfo f = new FileInfo(ApplicationPath);
- return f.FullName + "\\configs";
- //return f.FullName + "/configs";
- }
- }
- public static string ApplicationPictureDir
- {
- get
- {
- FileInfo f = new FileInfo(ApplicationPath);
- return f.FullName + "\\pictures";
- //return f.FullName + "/configs";
- }
- }
- public static string ApplicationResourcesDir
- {
- get
- {
- FileInfo f = new FileInfo(ApplicationPath);
- return f.FullName + "\\resources";
- }
- }
- public static string ApplicationLogDir
- {
- get
- {
- FileInfo f = new FileInfo(ApplicationPath);
- return f.FullName + "\\logs";
- }
- }
- /// <summary>
- /// 临时文件路径
- /// </summary>
- public static string ApplicationTempResources
- {
- get
- {
- FileInfo f = new FileInfo(ApplicationPath);
- string path = f.FullName + "\\temp";
- if (!System.IO.Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- return path;
- }
- }
- /// <summary>
- /// APPLow
- /// </summary>
- public static string AppLocalLowPath
- {
- get
- {
- return Path.Combine(Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)), "LocalLow");
- }
- }
- /// <summary>
- /// 系统帮助文档路径
- /// </summary>
- public static string ApplicationHelpDocument
- {
- get
- {
- FileInfo f = new FileInfo(ApplicationPath);
- return f.FullName + "\\document\\Help.pdf";
- }
- }
- internal class FileTimeInfo
- {
- public string FileName; //文件名
- public DateTime FileCreateTime; //创建时间
- public FileInfo FileInfo;
- }
- /// <summary>
- /// 获取目录下指定后缀的文件
- /// </summary>
- /// <param name="dir"></param>
- /// <param name="ext"></param>
- /// <returns></returns>
- public static List<FileInfo> FilesInDir(string dir, string ext)
- {
- List<FileInfo> list = new List<FileInfo>();
- DirectoryInfo d = new DirectoryInfo(dir);
- foreach (FileInfo fi in d.GetFiles())
- {
- if (string.IsNullOrEmpty(ext) || fi.Extension.ToUpper() == ext.ToUpper())
- {
- list.Add(fi);
- }
- }
- return list;
- }
- /// <summary>
- /// 获取某个路径下的最新文件
- /// </summary>
- /// <param name="dir"></param>
- /// <param name="ext"></param>
- /// <returns></returns>
- public static FileInfo FileInDirLastCreated(string dir, string ext, string extAnother = null)
- {
- if (!Directory.Exists(dir))
- {
- throw new FileNotFoundException(dir + " 未找到");
- }
- FileTimeInfo fi = GetLatestFileTimeInfo(dir, ext, extAnother);
- return new FileInfo(fi.FileName);
- }
- //获取最近创建的文件名和创建时间
- //如果没有指定类型的文件,返回null
- static FileTimeInfo GetLatestFileTimeInfo(string dir, string ext, string extAnother = null)
- {
- List<FileTimeInfo> list = new List<FileTimeInfo>();
- DirectoryInfo d = new DirectoryInfo(dir);
- foreach (FileInfo fi in d.GetFiles())
- {
- if (string.IsNullOrEmpty(ext) || fi.Extension.ToUpper() == ext.ToUpper())
- {
- list.Add(new FileTimeInfo()
- {
- FileName = fi.FullName,
- FileCreateTime = fi.CreationTime,
- FileInfo = fi
- });
- }
- }
- if (list.Count == 0)
- {
- foreach (FileInfo fi in d.GetFiles())
- {
- if (string.IsNullOrEmpty(extAnother) || fi.Extension.ToUpper() == extAnother.ToUpper())
- {
- list.Add(new FileTimeInfo()
- {
- FileName = fi.FullName,
- FileCreateTime = fi.CreationTime,
- FileInfo = fi
- });
- }
- }
- }
- var qry = from x in list
- orderby x.FileCreateTime
- select x;
- return qry.LastOrDefault();
- }
- public static void ClearFolder(string dir)
- {
- foreach (string d in Directory.GetFileSystemEntries(dir))
- {
- if (System.IO.File.Exists(d))
- {
- FileInfo fi = new FileInfo(d);
- if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
- fi.Attributes = FileAttributes.Normal;
- System.IO.File.Delete(d);//直接删除其中的文件
- }
- else
- {
- DirectoryInfo d1 = new DirectoryInfo(d);
- if (d1.GetFiles().Length != 0)
- {
- ClearFolder(d1.FullName);////递归删除子文件夹
- }
- Directory.Delete(d);
- }
- }
- }
- }
- }
|