Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用通配符存档并从文件夹中删除文件 让我们考虑一下, E:/Folder2/Fol24/Fo2222 E:/Folder3/Fol23/Folder333 E:/Folder4/Folde25/Fold44 E:/Folder5/Folder55/Folde5_C#_.net_Wildcard - Fatal编程技术网

C# 使用通配符存档并从文件夹中删除文件 让我们考虑一下, E:/Folder2/Fol24/Fo2222 E:/Folder3/Fol23/Folder333 E:/Folder4/Folde25/Fold44 E:/Folder5/Folder55/Folde5

C# 使用通配符存档并从文件夹中删除文件 让我们考虑一下, E:/Folder2/Fol24/Fo2222 E:/Folder3/Fol23/Folder333 E:/Folder4/Folde25/Fold44 E:/Folder5/Folder55/Folde5,c#,.net,wildcard,C#,.net,Wildcard,我需要使用一些输入,如E:/Folder?/Fol2?/Fo* 这里我期待着文件夹E:/Folder2/Fol24/Fo2222和E:/Folder3/Fol23/Folder333 这里的问题是用户可以输入任何通配符模式。根据模式,我必须将文件复制到其他位置并删除文件。在这里,我不能使用任何固定的搜索模式。它应该是动态的,可以有任意数量的通配符 谢谢你的帮助 问候 钱德拉私有静态列表文件夹=新列表(); 私有无效按钮示例\u单击(对象发送者,事件参数e) { 文件夹。清除(); 字符串输入=t

我需要使用一些输入,如
E:/Folder?/Fol2?/Fo*
这里我期待着文件夹
E:/Folder2/Fol24/Fo2222
E:/Folder3/Fol23/Folder333

这里的问题是用户可以输入任何通配符模式。根据模式,我必须将文件复制到其他位置并删除文件。在这里,我不能使用任何固定的搜索模式。它应该是动态的,可以有任意数量的通配符

谢谢你的帮助

问候 钱德拉

私有静态列表文件夹=新列表();
私有无效按钮示例\u单击(对象发送者,事件参数e)
{
文件夹。清除();
字符串输入=textBox1.Text;
string currentDir=string.Empty,currentPattern=string.Empty,nextPattern=string.Empty;
foreach(输入中的字符c)
{
如果(c=='?')
{
字符串[]f=input.Split(新的[]{'?'},2);
如果(f[0]。长度>0)
{
currentDir=Directory.GetParent(f[0]).FullName;
}
如果(f[1]。长度>0)
{
nextPattern=f[1]。TrimStart(“\\”);
}
字符串[]w=input.Split('\\');
foreach(w中的字符串s)
{
如果(s.包含(“?”))
{
电流模式=s;
打破
}
}
打破
}
else如果(c=='*')
{
string[]f=input.Split(新的[]{'*},2);
如果(f[0]。长度>0)
{
currentDir=Directory.GetParent(f[0]).FullName;
}
如果(f[1]。长度>0)
{
nextPattern=f[1]。TrimStart(“\\”);
}
字符串[]w=input.Split('\\');
foreach(w中的字符串s)
{
如果(s.包含('*'))
{
电流模式=s;
打破
}
}
打破
}
}
DirectoryInfo di=新的DirectoryInfo(currentDir);
List foldersNew=新列表();
foldersNew=GetAllFolders(di、currentPattern、nextPattern);
}
私有静态列表GetAllFolders(目录信息currentDir、字符串currentPattern、字符串nextPatten)
{
DirectoryInfo[]dis=currentDir.GetDirectories(currentPattern);
if(dis.Length==0&&string.Equals(currentPattern,string.Empty))
文件夹。添加(currentDir.FullName);
如果(dis.Length>0)
{
字符串[]remainPattern=nextPatten.Split(“\\”.ToCharArray());
如果(remainPattern.Length>0)
{
foreach(dis中的目录信息di)
{
GetAllFolders(di,remainPattern.First(),
Join(“\\”,remainPattern.Skip(1.ToArray());
}
}
}
返回文件夹;
}
我的解决方案可能对那些需要帮助的人有所帮助。这将解决问题。谢谢你的帮助


引用自:

您可以限制用户可以输入的通配符类型,并且在代码隐藏、foreach文件夹或文件字段中,您可以将通配符“转换”为正则表达式以验证它们。
    private static List<string> folders = new List<string>();

    private void buttonExample_Click(object sender, EventArgs e)
    {
        folders.Clear();

        string input = textBox1.Text;
        string currentDir = string.Empty, currentPattern = string.Empty, nextPattern = string.Empty;

        foreach (char c in input)
        {
            if (c == '?')
            {

                string[] f = input.Split(new[] {'?'}, 2);
                if (f[0].Length > 0)
                {
                    currentDir = Directory.GetParent(f[0]).FullName;
                }

                if (f[1].Length > 0)
                {
                    nextPattern = f[1].TrimStart('\\');
                }

                string[] w = input.Split('\\');
                foreach (string s in w)
                {
                    if (s.Contains('?'))
                    {
                        currentPattern = s;
                        break;
                    }
                }

                break;
            }
            else if(c == '*')
            {
                string[] f = input.Split(new[] { '*' }, 2);

                if (f[0].Length > 0)
                {
                    currentDir = Directory.GetParent(f[0]).FullName;
                }
                if (f[1].Length > 0)
                {
                    nextPattern = f[1].TrimStart('\\');
                }

                string[] w = input.Split('\\');
                foreach (string s in w)
                {
                    if (s.Contains('*'))
                    {
                        currentPattern = s;
                        break;
                    }
                }

                break;
            }
        }
        DirectoryInfo di = new DirectoryInfo(currentDir);
        List<string> foldersNew = new List<string>();
        foldersNew = GetAllFolders(di, currentPattern, nextPattern);
    }

    private static List<string> GetAllFolders(DirectoryInfo currentDir,string currentPattern, string nextPatten)
    {
        DirectoryInfo[] dis = currentDir.GetDirectories(currentPattern);
        if (dis.Length == 0 && string.Equals(currentPattern,string.Empty))
            folders.Add(currentDir.FullName);

        if (dis.Length > 0)
        {
            string[] remainPattern = nextPatten.Split("\\".ToCharArray());
            if (remainPattern.Length > 0)
            {
                foreach (DirectoryInfo di in dis)
                {
                    GetAllFolders(di, remainPattern.First(),
                                   string.Join("\\", remainPattern.Skip(1).ToArray()));
                }
            }

        }

        return folders;
    }