日期:2014-05-18 浏览次数:20485 次
while (true) { path = getDirectory(sServerDir);//修改 string directory = Path.Combine(sServerDir, path); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } sFileName=new Random().Next().ToString() + "_" + sFileName; string sFilePath = System.IO.Path.Combine(directory, sFileName); if (System.IO.File.Exists(sFilePath)) { iCounter++; sFileName = System.IO.Path.GetFileNameWithoutExtension(oFile.FileName) + "_" + iCounter + "." + sExtension; iErrorNumber = 201; } else { oFile.SaveAs(sFilePath); break; } } /// <summary> /// 获取文件夹路径 /// </summary> /// <param name="path">路径</param> /// <returns></returns> private string getDirectory(string path) { string directoryName = DateTime.Now.ToString("yyyyMMdd");//文件夹名称,每天一个 string filePath = Path.Combine(path, directoryName); if (!Directory.Exists(filePath)) { return Path.Combine(directoryName, getNumber(1)); } return Path.Combine(directoryName, getPath(Directory.GetDirectories(filePath))); } /// <summary> /// 获取文件夹中的文个数件少于1000的文件夹名称 /// </summary> /// <param name="info"></param> /// <returns></returns> private string getPath(string[] info) { DirectoryInfo directoryInfo = null; string fileName = string.Empty; if (info == null) { return null; } if (info.Length <= 0) { return getNumber(1); } for (int i = 0; i < info.Length; i++) { directoryInfo = new DirectoryInfo(info[i]); if (directoryInfo.Exists) { if (directoryInfo.GetFiles().Length < 1000) { fileName = info[i]; break; } } } if (string.IsNullOrEmpty(fileName)) { return getNumber(int.Parse(info.ToList<string>().Max(s => s.Substring(s.LastIndexOf("\\") + 1).TrimStart('0'))) + 1); } else { return fileName.Substring(fileName.LastIndexOf("\\")+1); } } /// <summary> /// 获取数字字符串,不足4位用0补足 /// </summary> /// <param name="number">不大于9999的数字</param> /// <returns></returns> private string getNumber(int number) { if (number > 9999) return null; return (10000 + number).ToString().Substring(1); }