110,534
社区成员
发帖
与我相关
我的任务
分享
var directory = new DirectoryInfo(@"C:\Users\彭迁\Documents\Visual Studio 2013\Projects\Test.Linq\ConsoleApplication1\bin\Debug");
var day = DateTime.Now.ToString("yyMMdd");
directory.GetFileSystemInfos().Where(x => x.Attributes != FileAttributes.Directory && x.Name.StartsWith(day)).Select(x => x.Name.Substring(0, x.Name.LastIndexOf(".", StringComparison.Ordinal)).Replace(day, "")).ToList().ForEach(Console.WriteLine);
var filesIndex = directory.GetFileSystemInfos().Where(x => x.Attributes != FileAttributes.Directory && x.Name.StartsWith(day)).Select(x => int.Parse(x.Name.Substring(0, x.Name.LastIndexOf(".", StringComparison.Ordinal)).Replace(day, ""))).OrderByDescending(x => x);
int index;
if (filesIndex.Any())
{
index = filesIndex.First();
index++;
}
else
{
index = 1;
}
System.IO.File.Create(string.Format(@"C:\Users\彭迁\Documents\Visual Studio 2013\Projects\Test.Linq\ConsoleApplication1\bin\Debug\{0}.txt", day + index.ToString("D6")));
经过测试,最后截取字符串出现问题 现在可以了
var directory = new DirectoryInfo(path);
var day = DateTime.Now.ToString("yyMMdd");
var filesIndex = directory.GetFileSystemInfos().Where(x => x.Attributes != FileAttributes.Directory && x.Name.StartsWith(day)).Select(x => int.Parse(x.Name.Replace(day,"").Substring(x.Name.LastIndexOf(".", StringComparison.Ordinal)))).OrderByDescending(x => x);
var index = 0;
if (filesIndex.Any())
{
index = filesIndex.First();
index++;
}
else
{
index = 1;
}
System.IO.File.Create(string.Format(@"path\{0}", day + index.ToString("D6")));
private void CreateFileName()
{
string path = @"E:\SVN";
DirectoryInfo folder = new DirectoryInfo(path);
List<string> list = new List<string>();
List<int> listSear = new List<int>();
foreach (FileInfo file in folder.GetFiles("*.txt"))
{
list.Add(Path.GetFileNameWithoutExtension(file.FullName));
listSear.Add(Convert.ToInt32(Path.GetFileNameWithoutExtension(file.FullName).Substring(6)));
}
string now = list.Find(p => p.Substring(0, 6) == DateTime.Now.ToString("yyMMdd"));//.Substring(0,6);
int sear = listSear.Max();
if (string.IsNullOrEmpty(now)) //如果为空说明当前日期没有新建文件
{
FileStream fs1 = new FileStream(path + "\\" + DateTime.Now.ToString("yyMMdd") + "000001.txt", FileMode.Create, FileAccess.Write);//创建写入文件
StreamWriter sw = new StreamWriter(fs1);
sw.Close();
fs1.Close();
}
else
{
FileStream fs1 = new FileStream(path + "\\" + DateTime.Now.ToString("yyMMdd") + (++sear).ToString().PadLeft(6, '0') + ".txt", FileMode.Create, FileAccess.Write);//创建写入文件
StreamWriter sw = new StreamWriter(fs1);
sw.Close();
fs1.Close();
}
}
有那些需要改进的地方