如何跳过C#中的某些文件夹 [英] how to skip the certain folder in C#

查看:277
本文介绍了如何跳过C#中的某些文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C:\ Users \ for \ AppData \ Roaming \ Mozilla \ Firefox \ Profiles \ fl43ywmg.default \ places.SQlite

我想跳过fl43ywmg.default文件夹以获取位置.Windows应用程序中的SQlite详细信息.

C:\Users\for\AppData\Roaming\Mozilla\Firefox\Profiles\fl43ywmg.default\places.SQlite

i want to skip the fl43ywmg.default folder to get the places.SQlite details in the Windows applications

推荐答案

不,您不能跳过必须写入的文件夹顺序.您肯定必须编写整个字符串,以表示资源文件(SQLite数据库)所在的目录.否则,您将得到错误 FileNotFoundException [ ^ ]等.这是因为在使用第三方文件夹时,您需要为其提供完整路径.基于操作系统的文件夹可能会被跳过.

另一个 answer [移动(使用System.IO命名空间的File.Move()方法) [
No you cannot skip the sequence of folders that have to be written. You would definitely have to write the entire string denoting the directory where your resource file (SQLite database) is present. Otherwise, you will get the error, FileNotFoundException[^] or so on and so forth. That is because while working with the third-party folders you are required to give a full-path to them. OS based folders, might be skipped.

Another answer[^], tells the same.

If anyways, you do not want your file to be present there, you can always Move (using the File.Move() method of System.IO namespace)[^] your file from one location to another location in order to use it over there. I believe this is a Firefox extension or dependency file I would like to recommend that you do not perform anything with this file, until or unless you know of any side-effects of this action on the extension or Firefox browser itself.


假设您的目标是解析目录中文件的内容:

C:\ Users \ for \ AppData \ Roaming \ Mozilla \ Firefox \ Profiles

并且,找到第一个包含名为``places.SQlite 其名称不是的文件"的文件夹:''fl43ywmg.default:
Assuming your goal is to parse the contents of the files in the Directory:

C:\Users\for\AppData\Roaming\Mozilla\Firefox\Profiles

And, find the first folder that contains a file named ''places.SQlite whose name is not: ''fl43ywmg.default:
// required
using System.IO;
using System.Linq;

// directories to ignore
private List<string> IgnoreDirectories = new List<string>
{
    "fl43ywmg.default", "Aunt Betty's Brown Bread", "Papa's Baked Beans"
}; 

private string basePath = @"C:\Users\for\AppData\Roaming\Mozilla\Firefox\Profiles\";

private void TestFindDirectory()
{
    // EnumerateDirectories filter does not implement regular expressions !
    string searchFilter = "*";

    var directories = Directory.EnumerateDirectories(basePath, searchFilter,
        SearchOption.AllDirectories).Select(dir => dir.Replace(searchFolderPath, ""));

    // for testing only
    //foreach (string str in directories)
    //{
        //Console.WriteLine("{0} {1}", str, IgnoreDirectories.Contains(str));
    //}
    // end for testing only

   // the IEnumerable<string> created above is only evaluated here !
    string directoryName = directories.First(dir => (! IgnoreDirectories.Contains(dir)));
}

注释:根据您希望从枚举文件中获取的符合您条件的文件中的哪些信息,您可能希望考虑使用''DirectoryInfo.EnumerateDirectories搜索目录的枚举FileInfo.您可以在此处进行一些更改轻松地替代它.

Comment: depending on what information you wish to get from the enumerated file, or files, that match your criteria, you may wish to consider searching the enumerated FileInfo of Directories using ''DirectoryInfo.EnumerateDirectories. You could easily substitute using that here with a few changes.


这篇关于如何跳过C#中的某些文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆