0

私はpdfをtif画像ファイルに変換するプロジェクトを持っています。また、出力ファイルにはフォームに番号が付けられます。ファイル1、ファイル2、ファイル3.......ファイル20。以下のコードを実行してファイルを取得すると、以下に示すようにリストに配置されますが、これは正しくありません。これを回避する方法はありますか?

FileInfo[] finfos = di.GetFiles("*.*");

finfos[0]=file1

finfos[1]=file10

finfos[2]=file11

finfos[3]=file12

....
...................

finfos[4]=file19

finfos[5]=file2

finfos[6]=file20

finfos[7]=file3

finfos[7]=file4
4

3 に答える 3

1

すべてのファイルに名前が付けられmypic<number>.tifていて、ディレクトリ内に異なる名前形式のファイルがない場合は、次を試してください。

        FileInfo[] orderedFI = finfos
            .OrderBy(fi => 
                // This will convert string representation of a number into actual integer
                int.Parse(
                    // this will extract the number from the filename
                    Regex.Match(Path.GetFileNameWithoutExtension(fi.Name), @"(\d+)").Groups[1].Value
                    ))
            .ToArray();
于 2013-03-07T08:32:30.210 に答える
0

それらが順番に作成されている場合は、作成日で並べ替えます。

これがリストを使用した問題の解決策です

class Program
{
    private static int CompareWithNumbers(FileInfo x, FileInfo y)
    {
        if (x == null)
        {
            if (y == null)
            {
                // If x is null and y is null, they're 
                // equal.  
                return 0;
            }
            else
            {
                // If x is null and y is not null, y 
                // is greater.  
                return -1;
            }
        }
        else
        {
            // If x is not null... 
            // 
            if (y == null)
            // ...and y is null, x is greater.
            {
                return 1;
            }
            else
            {

                int retval = x.CreationTime<y.CreationTime?-1:1;
                return retval;          

            }
        }
    }
    static void Main(string[] args)
    {
        DirectoryInfo di = new DirectoryInfo("d:\\temp");
        List<FileInfo> finfos = new List<FileInfo>();
        finfos.AddRange(di.GetFiles("*"));
        finfos.Sort(CompareWithNumbers);

        //you can do what ever you want
    }
}
于 2013-03-07T08:00:14.793 に答える
0

先行ゼロが解決策になる場合があります。ファイルを生成するコードを制御する場合、説明からは明確ではありません。そうでない場合は、file1、... file9 (つまり、正規表現またはファイル名の長さ) に一致する方法を使用して、それらの名前を変更できます。コードを制御する場合は、フォーマッタを使用して数値を先行ゼロ付きの文字列に変換します (つまり、2 桁の数値 {0:00} の場合)。

編集:

ディレクションを取得するには、次のドラフト サンプルを使用します。

実行ディレクトリに次のファイルがあるとします: file1.txt、file2.txt、file10.txt、および file20.txt

foreach (string fn in System.IO.Directory.GetFiles(".", "file*.*"))
  if (System.Text.RegularExpressions.Regex.IsMatch(fn, @"file\d.txt"))
    System.IO.File.Move(fn, fn.Replace("file", "file0"));

上記のコードは、file1.txt を file01.txt に、file2.txt を file02.txt に名前変更します。

于 2013-03-07T08:57:01.893 に答える