.NETでOpenFileDialogを使用して、ファイル名(1000 +ファイルの複数選択)を取得する方法

StackOverflow https://stackoverflow.com/questions/2607596

  •  25-09-2019
  •  | 
  •  

質問

たぶん、あなたのいくつかは、この遭遇してきた前に....

私は、構文解析のためにファイルを開封しております。私はもちろん、OpenFileDialogを使用していますが、私は.FileNames列に2048のバッファに制限されています。したがって、私は、数百のファイルを選択することができます。これは、ほとんどの場合OKです。しかし、フォアの例では、私は1つの場合に開くように1400個のファイルを持っています。あなたは、ファイルを開くダイアログでこれを行う方法を知っていますか。私はちょうど.FileNamesの文字列配列をしたい、私はパーサクラスにそれを渡します。

私はまた、FolderBrowserDialogオプションを提供することを考えていたし、私はDirectoryInfoクラスのように、ディレクトリ内のすべてのファイルを介しちょうどループに他のいくつかの方法を使用すると思います。私は1つの、溶液中のすべてを持つことができない場合、私は最後の手段としてこれを行うと思います。

役に立ちましたか?

解決 4

私は何をやってしまったことの方法を書いていたがOpenFileDialogを使用しますが、間接的にパス文字列の長さをチェックします。メソッドが失敗した場合には、エラーがあまりにも多くのファイルがあることを告げる、ユーザに表示され、その後、FolderBrowserは、ユーザーが探していたことを選択したフォルダに示されている。私はまた、インポートファイルまたはインポートに別々のオプションを追加しましたメニューバー内のフォルダます。

ここでそれを行うためのコードです。これらは、など、私はエクセルやアクセスまたはXMLへの書き込みのため、すべてのカスタマイズIOのものを入れDataFileIOという静的クラスのメソッドです。

        public static string[] GetFiles()
    {
        string[] fileNames;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = UniversalDataImporter.Properties.Settings.Default.openFilePath;
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = false;
        openFileDialog1.Multiselect = true;
        openFileDialog1.CheckFileExists = false;

        try
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK && openFileDialog1.FileNames.Count() <501 )
            {
                UniversalDataImporter.Properties.Settings.Default.openFilePath =
                    Path.GetDirectoryName(openFileDialog1.FileName);
                UniversalDataImporter.Properties.Settings.Default.Save();
                return fileNames = openFileDialog1.FileNames;
            }
            else if (result == DialogResult.Cancel)
            {
                return null;
            }
            else
            {
                if (MessageBox.Show("Too many files were Selected. Would you like to import a folder instead?",
                    "Too many files...", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    return fileNames = GetFilesInFolder();
                }
                else
                {
                    return null;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            return null;
        }
    }

    public static string[] GetFilesInFolder()
    {

        FileInfo[] fileInfo;

        string pathName;
        FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

        folderBrowserDialog.RootFolder = System.Environment.SpecialFolder.Desktop;

        DialogResult results = folderBrowserDialog.ShowDialog();

        if (results == DialogResult.OK)
        {
            try
            {
                pathName = folderBrowserDialog.SelectedPath;

                DirectoryInfo dir = new DirectoryInfo(pathName);
                if (dir.Exists)
                {

                    fileInfo = dir.GetFiles();

                    string[] fileNames = new string[fileInfo.Length];

                    for (int i = 0; i < fileInfo.Length; i++)//this is shit
                    {
                        fileNames[i] = fileInfo[i].FullName;
                    }

                    return fileNames;
                }
                else
                {
                    return null;
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                return null;
            }

        }
        else if (results == DialogResult.Cancel) 
        {
            return null;
        }
        else { return null; }
    }

他のヒント

私おやっ私は、ファイルを開くダイアログで1400個のファイルを選択すると想像することはできません。おそらく、あなただけのフィルタでキーをユーザーに許可し、その後System.IO.Directory.GetFiles呼び出しを行う必要があります。

私は間違いなくFolderBrowserルートを行くと思います。私は手動で50-100はるかに少ない1000 +のファイルを選択する必要がありますする必要がありませんでした。ベターはそのように一致し、それらを選択するためにいくつかのパターンのプロンプト、フォルダを取得します。使いやすさの観点から、大量のファイルを選択すると、悪い選択の私見です。

あなたは、任意のエラーまたは例外を得るのですか?あなたはOpenFileDialogfrom System.Windows.Forms名前空間を使用していることを確信していますか?

次のコードは、選択された2000の以上のファイルを完璧に動作します:

System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.InitialDirectory = @"C:\Windows\system32\";
ofd.Multiselect = true;
ofd.ShowDialog();

foreach (var file in ofd.FileNames)
{
    Trace.WriteLine(file);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top