提问者:小点点

OpenFileDialog中的文件名模式无效


我几天前开始学习编程,我正在一个同时复制和粘贴几个文件的程序上工作/练习,但是当要让它在多个不同的扩展上工作时,我就遇到了麻烦

这是密码

null

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;

null

我该怎么办?


共1个答案

匿名用户

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

// 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));
    }
}