コード例 #1
0
        public static string GetNewFilename(string path, bool forceNewFilename = false)
        {
            if (forceNewFilename || zFile.Exists(path))
            {
                //string filename = zPath.GetFileNameWithoutExtension(path);
                //string ext = zPath.GetExtension(path);
                //string directory = zPath.GetDirectoryName(path);
                //for (int i = 1; ; i++)
                //{
                //    path = zPath.Combine(directory, string.Format("{0}[{1}]{2}", filename, i, ext));
                //    if (!zFile.Exists(path))
                //        break;
                //}

                FileNumber fileNumber = FileNumber.GetFileNumber(path);
                for (int i = fileNumber.Number + 1; ; i++)
                {
                    path = fileNumber.GetPath(i);
                    if (!zFile.Exists(path))
                    {
                        break;
                    }
                }
            }
            return(path);
        }
コード例 #2
0
ファイル: Directory.cs プロジェクト: 24/source_04
        // followDirectoryTree : followDirectoryTree est appelé quand on entre dans un répertoire et quand on en sort
        //                       à l'entrée EnumDirectoryInfo contient les info du répertoire
        //                       à la sortie EnumDirectoryInfo contient uniquement le Level
        // followDirectoryTree exemple EnumerateDirectoriesInfo(@"c:\toto");
        //   entré dans tata EnumDirectoryInfo = Directory: "c:\toto\tata" SubDirectory: "tata" Level: 1
        //   entré dans tutu EnumDirectoryInfo = Directory: "c:\toto\tata\tutu" SubDirectory: "tutu" Level: 2
        //   sortie de tutu EnumDirectoryInfo = Directory: null SubDirectory: null Level: 2
        //   sortie de tata EnumDirectoryInfo = Directory: null SubDirectory: null Level: 1
        public static IEnumerable <EnumDirectoryInfo> EnumerateDirectoriesInfo(string directory, string pattern = null,
                                                                               //Func<EnumDirectoryInfo, EnumDirectoryFilter> directoryFilter = null,
                                                                               IEnumerable <Func <EnumDirectoryInfo, EnumDirectoryFilter> > directoryFilters = null,
                                                                               //Action<EnumDirectoryInfo> followDirectoryTree = null,
                                                                               IEnumerable <Action <EnumDirectoryInfo> > followDirectoryTrees = null,
                                                                               bool getSubDirectoryNumber = false)
        {
            if (!zDirectory.Exists(directory))
            {
                yield break;
            }
            Stack <EnumDirectory> directoryStack      = new Stack <EnumDirectory>();
            EnumDirectory         enumDirectory       = new EnumDirectory(directory, pattern);
            EnumDirectoryFilter   enumDirectoryFilter = new EnumDirectoryFilter {
                Select = true, RecurseSubDirectory = true
            };
            int l     = directory.Length + 1;
            int level = 1;

            while (true)
            {
                if (enumDirectory.directoryEnum.MoveNext())
                {
                    DirectoryInfo     directoryInfo     = enumDirectory.directoryEnum.Current;
                    EnumDirectoryInfo enumDirectoryInfo = new EnumDirectoryInfo
                    {
                        Directory = directoryInfo.FullName, SubDirectory = directoryInfo.FullName.Substring(l), Name = directoryInfo.Name, Level = level
                    };

                    if (getSubDirectoryNumber)
                    {
                        FileNumber directoryNumberInfo = FileNumber.GetFileNumber(enumDirectoryInfo.Name);
                        enumDirectoryInfo.Name   = directoryNumberInfo.BasePath;
                        enumDirectoryInfo.Number = directoryNumberInfo.Number;
                    }

                    //if (directoryFilter != null)
                    //    enumDirectoryFilter = directoryFilter(enumDirectoryInfo);
                    if (directoryFilters != null)
                    {
                        enumDirectoryFilter = EvaluateDirectoryFilters(directoryFilters, enumDirectoryInfo);
                    }

                    if (enumDirectoryFilter.Select)
                    {
                        //if (followDirectoryTree != null)
                        //    followDirectoryTree(enumDirectoryInfo);
                        if (followDirectoryTrees != null)
                        {
                            FollowDirectoryTrees(followDirectoryTrees, enumDirectoryInfo);
                        }

                        //if (getSubDirectoryNumber)
                        //{
                        //    FilenameNumberInfo directoryNumberInfo = FilenameNumberInfo.GetFilenameNumberInfo(enumDirectoryInfo.SubDirectory);
                        //    //enumDirectoryInfo.SubDirectory = directoryNumberInfo.BaseFilename;
                        //    enumDirectoryInfo.SubDirectory = directoryNumberInfo.BasePath;
                        //    enumDirectoryInfo.Number = directoryNumberInfo.Number;
                        //}
                        yield return(enumDirectoryInfo);

                        //if (!enumDirectoryFilter.RecurseSubDirectory && followDirectoryTree != null)
                        //    followDirectoryTree(new EnumDirectoryInfo { Level = level });
                        if (!enumDirectoryFilter.RecurseSubDirectory && followDirectoryTrees != null)
                        {
                            FollowDirectoryTrees(followDirectoryTrees, new EnumDirectoryInfo {
                                Level = level
                            });
                        }
                    }

                    if (enumDirectoryFilter.RecurseSubDirectory)
                    {
                        //if (!enumDirectoryFilter.Select && followDirectoryTree != null)
                        //    followDirectoryTree(enumDirectoryInfo);
                        if (!enumDirectoryFilter.Select && followDirectoryTrees != null)
                        {
                            FollowDirectoryTrees(followDirectoryTrees, enumDirectoryInfo);
                        }

                        directoryStack.Push(enumDirectory);
                        enumDirectory = new EnumDirectory(enumDirectory.directoryEnum.Current, pattern);
                        level++;
                    }
                }
                else
                {
                    if (directoryStack.Count == 0)
                    {
                        break;
                    }
                    enumDirectory = directoryStack.Pop();
                    level--;

                    //if (followDirectoryTree != null)
                    //    followDirectoryTree(new EnumDirectoryInfo { Level = level });
                    if (followDirectoryTrees != null)
                    {
                        FollowDirectoryTrees(followDirectoryTrees, new EnumDirectoryInfo {
                            Level = level
                        });
                    }
                }
            }
        }