关于c#:文件名模式在OpenFileDialog中不起作用

Filename pattern not working in OpenFileDialog

几天前我开始学习编程,我正在一个同时复制和粘贴多个文件的程序上工作/练习,但要使它在多个不同的扩展名上工作时遇到了困难。

下面是代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
namespace practice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public string[] getFlies
        {
            get;
            set;
        }
        public string getdirectory
        {
            get;
            set;
        }
        public string[] getextension
        {
            get;
            set;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog getfiles = new OpenFileDialog();
                getfiles.Filter ="All Files (.)|.";
                getfiles.FilterIndex = 1;
                getfiles.Multiselect = true;
</p>

<wyn>            if (getfiles.ShowDialog() == DialogResult.OK)
            {
                getFlies = getfiles.FileNames;


                foreach (string file_name in getFlies)
                {
                    listBox1.Items.Add(file_name);
                    getextension = Path.GetExtension(getFlies);
                }
            }
        }
        catch
        {
            MessageBox.Show("Error");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fbb = new FolderBrowserDialog();
        fbb.Description ="Select a folder";

        if (fbb.ShowDialog() == DialogResult.OK)
        {
            getdirectory = fbb.SelectedPath;
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        foreach (object item in listBox1.Items)
        {
            File.Copy(getFlies.ToString(), getdirectory);
        }

    }

    private void button4_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
    }
}

我该怎么办?


我建议您将变量的名称改为更好的名称。但为了与代码保持一致,我使用了您选择的名称。好的,你可以这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// instead of string[].
public FileInfo[] getFlies { get; set; } // I suggest to change the name, maybe to Files?


// in your button1_click method, after the DialogResult is OK
// getFiles in the dialog and getFlies is the array.
public void button1_Click(object sender, EventArgs e)
{
    var getfiles = new OpenFileDialog();  // again, change the name...maybe for dialog?
    getfiles.Filter ="All Files (*.*)|*.*"; // I fixed it. You missed the '*' .
    // more dialog initialization

    if(Dialog.Result.OK == getfiles.ShowDialog())
    {
        getFlies = getfiles.FileNames.Select( f => new FileInfo(f)).ToArray();
    }
}


// Now, in button3_click
void button3_Click(object sender, EventArgs e)
{
    foreach(var file in getFlies)
    {
        File.Copy(file.FullName, Path.Combine(getdirectory, file.Name));
    }
}