示例#1
0
 /// <summary>
 /// 删除文件
 /// </summary>
 /// <param name="file">要删除的文件路径和名称</param>
 public static void DeleteFile(string file)
 {
     if (System.IO.File.Exists(PathHelper.CombineWithBaseDirectory(file)))
     {
         System.IO.File.Delete(PathHelper.CombineWithBaseDirectory(file));
     }
 }
示例#2
0
        public static bool SaveFile(this IFormFile file)
        {
            try
            {
                var filename = ContentDispositionHeaderValue
                               .Parse(file.ContentDisposition)
                               .FileName
                               .Trim('"');
                string newFileName = Path.GetFileNameWithoutExtension(filename) + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(filename);
                filename = PathHelper.CombineWithBaseDirectory(newFileName);

                using (FileStream fs = System.IO.File.Create(filename))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }

                return(true);
            }
            catch (Exception)
            {
                throw;
            }
            return(false);
        }
示例#3
0
 /// <summary>
 /// 复制文件
 /// </summary>
 /// <param name="dir1">要复制的文件的路径已经全名(包括后缀)</param>
 /// <param name="dir2">目标位置,并指定新的文件名</param>
 public static void CopyFile(string dir1, string dir2)
 {
     dir1 = dir1.Replace("/", "\\");
     dir2 = dir2.Replace("/", "\\");
     if (System.IO.File.Exists(PathHelper.CombineWithBaseDirectory(dir1)))
     {
         System.IO.File.Copy(PathHelper.CombineWithBaseDirectory(dir1), PathHelper.CombineWithBaseDirectory(dir2), true);
     }
 }
示例#4
0
 /// <summary>
 /// 创建文件
 /// </summary>
 /// <param name="dir">相对路径</param>
 /// <param name="pagestr">文件内容</param>
 public static void CreateFile(string dir, string pagestr)
 {
     dir = dir.Replace("/", "\\");
     if (dir.IndexOf("\\") > -1)
     {
         CreateDir(dir.Substring(0, dir.LastIndexOf("\\")));
     }
     System.IO.StreamWriter sw = new System.IO.StreamWriter(PathHelper.CombineWithBaseDirectory(dir), false, System.Text.Encoding.GetEncoding("utf-8"));
     sw.Write(pagestr);
     sw.Close();
 }
示例#5
0
 /// <summary>
 /// 删除目录
 /// </summary>
 /// <param name="dir">要删除的目录路径和名称</param>
 public static void DeleteDir(string dir)
 {
     if (dir.Length == 0)
     {
         return;
     }
     if (Directory.Exists(PathHelper.CombineWithBaseDirectory(dir)))
     {
         Directory.Delete(PathHelper.CombineWithBaseDirectory(dir));
     }
 }
示例#6
0
        public string GetLogPath(string customDirectory, string preFile)
        {
            string newFilePath = string.Empty;
            //String logDir = string.IsNullOrEmpty(customDirectory) ? Path.Combine(Environment.CurrentDirectory, "logs") : customDirectory;
            String logDir = string.IsNullOrEmpty(customDirectory) ? PathHelper.CombineWithBaseDirectory("logs") : customDirectory;

            if (!Directory.Exists(logDir))
            {
                Directory.CreateDirectory(logDir);
            }
            string        extension        = ".log";
            string        fileNameNotExt   = String.Concat(preFile, DateTime.Now.ToString("yyyyMMdd"));
            String        fileName         = String.Concat(fileNameNotExt, extension);
            string        fileNamePattern  = string.Concat(fileNameNotExt, "(*)", extension);
            List <string> filePaths        = new List <string>(Directory.GetFiles(logDir, fileNamePattern, SearchOption.TopDirectoryOnly));
            List <string> correctFilePaths = new List <string>();

            if (filePaths.Count > 0)
            {
                foreach (string fPath in filePaths)
                {
                    string no     = new Regex(@"(?is)(?<=\()(.*)(?=\))").Match(Path.GetFileName(fPath)).Value;
                    int    tempno = 0;
                    if (int.TryParse(no, out tempno))
                    {
                        correctFilePaths.Add(fPath);
                    }
                }
            }
            if (correctFilePaths.Count > 0)
            {
                correctFilePaths.Sort((x, y) => x.CompareTo(y));
                int fileMaxLen = 0;
                for (int i = 0; i < correctFilePaths.Count; i++)
                {
                    int itemLength = correctFilePaths[i].Length;
                    fileMaxLen = itemLength > fileMaxLen ? itemLength : fileMaxLen;
                }
                string lastFilePath = correctFilePaths.FindLast(d => d.Length == fileMaxLen);
                long   actualSize   = new FileInfo(lastFilePath).Length;
                long   maxSize      = 10 * 1024 * 1024;//(1 * 1024 * 1024 = 1M);
                if (actualSize < maxSize)
                {
                    newFilePath = lastFilePath;
                }
                else
                {
                    string no          = new Regex(@"(?is)(?<=\()(.*)(?=\))").Match(Path.GetFileName(lastFilePath)).Value;
                    int    tempno      = 0;
                    bool   parse       = int.TryParse(no, out tempno);
                    string formatno    = String.Format("({0})", parse ? (tempno + 1) : tempno);
                    string newFileName = String.Concat(fileNameNotExt, formatno, extension);
                    newFilePath = Path.Combine(logDir, newFileName);
                }
            }
            else
            {
                string newFileName = String.Concat(fileNameNotExt, String.Format("({0})", 0), extension);
                newFilePath = Path.Combine(logDir, newFileName);
            }
            return(newFilePath);
        }