如何避免在 vb.net 中对给定目录名称进行处理

How to avoid doing processes on a given directory name in vb.net

我正在制作一个程序,用于从文件目录或超过特定月数的文件中删除未经授权的文件。我遇到的问题是我还需要避免目录中有两个具有特定名称的目录,而且我不确定如何使用我正在使用的当前方法执行此操作。 任何建议将不胜感激,提前致谢!

编辑:尝试使用类似的代码,同时对文件而不是目录执行相同的操作,但结果不一样

 Dim dir1 As DirectoryInfo() = dir2.GetDirectories().Where(Name = Name!.Contains(("(0000000.00A)" & "(0000000.00B)")))  'Makes reference to the directory in dir2

下面的代码是我目前的运行:

Dim dir2 As New DirectoryInfo("D:\Administrator\Desktop\File Directories\File Directories[=12=]000000.00F")    
Dim dir1 As DirectoryInfo() = dir2.GetDirectories()

    Dim Auth(lstApprovedItems.Items.Count - 1) As String
    lstApprovedItems.Items.CopyTo(Auth, 0)

    Dim fri2 As DirectoryInfo
    For Each fri2 In dir1

        FileName1 = Convert.ToString(fri2.Name)     

        Dim dir0 As New DirectoryInfo("D:\Administrator\Desktop\File Directories\File Directories[=12=]000000.00F\" + FileName1) 'Makes reference to a directory   ##### Needs to be changed to relevant directory #####
        Dim fiArr As FileInfo() = dir0.GetFiles()     'Gets reference to each file in the directory in the 'dir' variable

        For Each fi As FileInfo In dir0.GetFiles

            If Auth.Contains(fi.Extension) Then

                CreatedDate = fi.CreationTime
                DueDate = DateDiff(DateInterval.Day, CreatedDate, CurrentDate) 

                If DueDate < 90 Then   

                    AuthList.Add(fi)

                Else       

             Dim MoveFile As String = "D:\Administrator\Desktop\File Directories\Archive\Archive 0.4\" + fi.ToString
                    NoAuthList.Add(fi)          'Adds the files to a no authorisation list
                    If File.Exists(MoveFile) Then      

                    Else           
                        fi.CopyTo(MoveFile)       
                        MovedFiles = MovedFiles + 1     
                    End If

                End If

            Else    

                 Dim MoveFile As String = "D:\Administrator\Desktop\File Directories\Archive\Archive 0.4\" + fi.ToString
                NoAuthList.Add(fi)          
                If File.Exists(MoveFile) Then       

                Else            
                    fi.CopyTo(MoveFile)             
                    MovedFiles = MovedFiles + 1                         End If

            End If
        Next

    Next

我会创建一个字典或集合对象,您可以从配置文件加载它,并在删除之前检查该文件或目录是否不在集合中。您实际上是在制作一份永远不应删除的项目的白名单,任何需要保护特定 files/directories 的管理员都可以随时对其进行修改。由于它是一个白名单并且仅用于保护特定 files/directories,因此您降低了有人修改文件并添加有问题的条目的可能性。尽管在尝试将项目加载到集合中之前,您仍然应该清理此文件的读取以确保它遵循正确的格式。

您可以创建一个函数来读取包含 files/directories 列表的文件。首先,您将编辑 App.config 文件并添加如下条目:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- ... -->
    <add key="exemptListFile" value="C:\path\to\exemptfiles.txt"/>
    <!-- ... -->
  </appSettings>
</configuration>

然后在引用的文本文件中添加每个条目,每行一个:

C:\Some\Protected\Directory\
C:\Some\Protected\File.txt

然后在项目的某处添加一个新函数:

public Collection<string> GetExemptPaths()
{
    Collection<string> paths = new Collection<string>();
    string filePath = System.Configuration.ConfigurationManager.AppSettings["exemptListFile"];
    if (!File.Exists(filePath)) return paths;
    using(StreamReader reader = File.OpenText(filePath))
    {
        while(!reader.EndOfStream)
        {
            string pathToAdd = reader.ReadLine();
            if (File.Exists(pathToAdd) || Directory.Exists(pathToAdd))
                paths.Add(pathToAdd);
        }
    }
    return paths;
}

然后只需调用该函数并将 return 集合分配给函数中的一个变量:

Collection<string> excludePaths = GetExemptPaths();

并检查您要删除的 file/directory 是否不在该集合中。