按序列生成文件名称

_小黑_ 2016-08-12 05:10:35
RT
如 文件 要创建在 A 文件 A文件夹 下 有 160810000001 那么 在创建就是 02以此类推
如果到09 那么再有文件 就是160810000010
名称的格式 就是 yyMMdd+六位流水号

如果 文件名称 160812000001
那么 隔天生成 应该是 160813000001
求大神解答
...全文
170 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
跳动de指尖 2016-08-12
  • 打赏
  • 举报
回复

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")));

经过测试,最后截取字符串出现问题 现在可以了
跳动de指尖 2016-08-12
  • 打赏
  • 举报
回复
引用 6 楼 pengqian098 的回复:

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")));
错了,应该这样 var filesIndex = directory.GetFileSystemInfos().Where(x => x.Attributes != FileAttributes.Directory && x.Name.StartsWith(day)).Select(x => int.Parse(x.Name.Replace(day, "").Substring(0, x.Name.LastIndexOf(".", StringComparison.Ordinal) + 1))).OrderByDescending(x => x);
跳动de指尖 2016-08-12
  • 打赏
  • 举报
回复

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")));
_小黑_ 2016-08-12
  • 打赏
  • 举报
回复
代码有问题 ........ 还需要改进 有个 bug
_小黑_ 2016-08-12
  • 打赏
  • 举报
回复

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();
            }
        }
有那些需要改进的地方
_小黑_ 2016-08-12
  • 打赏
  • 举报
回复
功能已经实现 如果是多线程的话 应该注意什么呢
胜负多少 2016-08-12
  • 打赏
  • 举报
回复
恩,排序是最重要的。。。其他的就是字符串转int,int转字符串的,字符串拼接的问题了。。
shoppo0505 2016-08-12
  • 打赏
  • 举报
回复
创建文件的时候,将当前日期转为yymmdd格式, 找出所有含有这个前缀的文件, 排序后,获取最后一个文件, 去掉前缀,将余下的字符串专int 结果+1, 余下的自己做吧

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧