C# 在文件夹中查找名称相同但扩展名不同的文件

C# 在文件夹中查找名称相同但扩展名不同的文件,c#,.net,file-io,compact-framework2.0,C#,.net,File Io,Compact Framework2.0,我有一个FTP服务器,它将客户端发送/上传的文件存储在某个文件夹中。客户端将上载3个名称相同但扩展名不同的文件。例如,客户端将发送file1.ext1、file1.ext2和file1.ext3。我正在寻找一段代码,它将帮助我找到同名文件(“file1”),然后压缩它们。感谢您的帮助。我编写了以下代码,该代码获取文件夹中所有文件的名称: string path = "somepath"; String[] FileNames = Directory.GetFiles(path); 您可以尝试下

我有一个FTP服务器,它将客户端发送/上传的文件存储在某个文件夹中。客户端将上载3个名称相同但扩展名不同的文件。例如,客户端将发送file1.ext1、file1.ext2和file1.ext3。我正在寻找一段代码,它将帮助我找到同名文件(“file1”),然后压缩它们。感谢您的帮助。我编写了以下代码,该代码获取文件夹中所有文件的名称:

string path = "somepath";
String[] FileNames = Directory.GetFiles(path);

您可以尝试下面的代码。这也将解决文件名本身是否有点的问题

        string[] fileNames = Directory.GetFiles(path);

        List<string> fileNamesWithoutExtension = new List<string>(); 
        List<string> myFiles = new List<string>();

        string myFile = mySearchFileWithoutExtension;

        foreach (string s in fileNames)
        {
            char[] charArray = s.ToCharArray();

            Array.Reverse(charArray);
            string s1 = new string(charArray);

            string[] ext = s1.Split(new char[] { '.' }, 2);

            if ((ext.Length > 1))
            {
                char[] charArray2 = ext[1].ToCharArray();

                Array.Reverse(charArray2);

                fileNamesWithoutExtension.Add(new string(charArray2));

                if ((new string(charArray2)).Trim().Equals(myFile))
                {
                    myFiles.Add(s);
                }
            }
        }
string[]fileNames=Directory.GetFiles(path);
List fileNamesWithoutExtension=new List();
List myFiles=new List();
字符串myFile=mySearchFileWithoutExtension;
foreach(文件名中的字符串s)
{
char[]charArray=s.ToCharArray();
数组。反向(charArray);
字符串s1=新字符串(字符集);
字符串[]ext=s1.Split(新字符[]{.'},2);
如果((外部长度>1))
{
char[]charArray2=ext[1]。ToCharArray();
数组。反向(charArray2);
fileNamesWithoutExtension.Add(新字符串(charArray2));
if((新字符串(chararry2)).Trim().Equals(myFile))
{
myFiles.Add;
}
}
}
不带扩展名的文件列表:filenamewithoutextension


所需的文件集:myFiles

在调用
GetFiles
时,使用星号通配符作为文件扩展名,例如:

 List<string> files = Directory.GetFiles(pathName, "SpecificFileName.*");

这相当简单:

string path = "somepath";
String[] FileNames = Directory.GetFiles(path);
您可以使用LINQ按文件名对文件进行分组,不带扩展名:

var fileGroups = from f in FileNames
    group f by Path.GetFileNameWithoutExtension(f) into g
    select new { Name = g.Key, FileNames = g };

// each group will have files with the
// same name and different extensions
foreach (var g in fileGroups)
{
    // initialize zip file
    foreach (var fname in g.FileNames)
    {
        // add fname to zip
    }
    // close zip file
}
更新 如果没有LINQ,任务也不会太难。首先,要对文件进行排序:

Array.Sort(FileNames);
现在,您有了一个文件列表,按文件名排序。例如,您将有:

file1.ext1
file1.ext2
file1.ext3
file2.ext1
file2.ext2
etc...
然后只需浏览列表,将具有相同基本名称的文件添加到zip文件中,如下所示。请注意,我不知道您是如何创建zip文件的,所以我只编写了一个简单的
ZipFile
类。你当然需要用你正在使用的任何东西来替换它

string lastFileName = string.Empty;
string zipFileName = null;
ZipFile zipFile = null;
for (int i = 0; i < FileNames.Length; ++i)
{
    string baseFileName = Path.GetFileNameWithoutExtension(FileNames[i]);
    if (baseFileName != lastFileName)
    {
        // end of zip file
        if (zipFile != null)
        {
            // close zip file
            ZipFile.Close();
        }
        // create new zip file
        zipFileName = baseFileName + ".zip";
        zipFile = new ZipFile(zipFileName);
        lastFileName = baseFileName;
    }
    // add this file to the zip
    zipFile.Add(FileNames[i]);
}
// be sure to close the last zip file
if (zipFile != null)
{
    zipFile.Close();
}

你试过读取目录文件以获得目录中的文件列表吗?你试过我的方法了吗?这是一条单行线@杰里米·汤普森:你的答案是“恰到好处”,但我不知道文件名。我必须编写一个在后台运行的方法,扫描文件夹,并从3个同名但扩展名不同的文件中创建zip文件。文件名由设备自动生成(我无法控制)。该设备还将这些文件FTPs到我的FTP服务器。文件名是随机字母数字字符串。这是最优雅的解决方案。但不幸的是,我正在.Net compact framework 2.0上开发。我无法在此环境中使用LINQ。请为此提供“无LINQ”版本,好吗?此行出现编译错误:
string[]ext=s1.Split(new char[]{'.},2)我已经测试了上面的代码。它正在工作,而且我已经根据您的要求进行了编辑。感谢您的帮助,但我在同一行仍然收到相同的编译错误。错误为:“与'string.Split(params char[])匹配的最佳重载方法有一些无效参数。只需替换为其他签名:s1.Split('。);方法中的其他行可能没有用处,但如果文件名中只有一个“.”,则可以解决问题。这将拾取
SpecificFileName.BlaBla.ext
string lastFileName = string.Empty;
string zipFileName = null;
ZipFile zipFile = null;
for (int i = 0; i < FileNames.Length; ++i)
{
    string baseFileName = Path.GetFileNameWithoutExtension(FileNames[i]);
    if (baseFileName != lastFileName)
    {
        // end of zip file
        if (zipFile != null)
        {
            // close zip file
            ZipFile.Close();
        }
        // create new zip file
        zipFileName = baseFileName + ".zip";
        zipFile = new ZipFile(zipFileName);
        lastFileName = baseFileName;
    }
    // add this file to the zip
    zipFile.Add(FileNames[i]);
}
// be sure to close the last zip file
if (zipFile != null)
{
    zipFile.Close();
}
string filename = @"c:\dir\subdir\file.ext";
int dotPos = filename.LastIndexOf('.');
int slashPos = filename.LastIndexOf('\\');
string ext;
string name;
int start = (slashPos == -1) ? 0 : slashPos+1;
int length;
if (dotPos == -1 || dotPos < slashPos)
    length = filename.Length - start;
else
    length = dotPos - start;
string nameWithoutExtension = filename.Substring(start, length);