使用C#替换目录中存在的文件

时间:2014-04-26 01:54:10

标签: c# file-upload

我想在C#Gui上传文件并检查它是否已存在于该特定文件夹中。但是,为什么只有第二个文件被替换,即使第一个文件已经存在?我有2个上传按钮。

两个按钮。

    private void upload_1_Click(object sender, EventArgs e)
    {
        OpenFileDialog file = new OpenFileDialog();
        file.Title = "Browse the File";
        file.Filter = "PDF Files (*.PDF)|*.PDF|" + "Images (*.BMP;*.JPEG;*.JPG;*.GIF;*.PNG;*.TIFF)|*.BMP;*.JPEG;*.JPG;*.GIF;*.PNG;*.TIFF|" +
        "All files (*.*)|*.*";
        if (file.ShowDialog() == DialogResult.OK)
        {
            string file_path = Path.GetDirectoryName(file.FileName.ToString());
            string file_obj = Path.GetFileName(file.FileName.ToString());
            string file_itself = file_path + "\\" + file_obj;
            upload_label1.Text = file_itself;

        }
    }

    private void upload_2_Click(object sender, EventArgs e)
    {
        OpenFileDialog file1 = new OpenFileDialog();
        file1.Title = "Browse the File";
        file1.Filter = "PDF Files (*.PDF)|*.PDF|" + "Images (*.BMP;*.JPEG;*.JPG;*.GIF;*.PNG;*.TIFF)|*.BMP;*.JPEG;*.JPG;*.GIF;*.PNG;*.TIFF|" +
        "All files (*.*)|*.*";
        if (file1.ShowDialog() == DialogResult.OK)
        {
            string file_path1 = Path.GetDirectoryName(file1.FileName.ToString());
            string file_obj1 = Path.GetFileName(file1.FileName.ToString());
            string file_itself1 = file_path1 + "\\" + file_obj1;
            upload_label2.Text = file_itself1;

        }
    }

更新按钮

    private void update_Click(object sender, EventArgs e)
    {
        try
        {
            string ext = upload_label1.Text;
            ext = ext.Substring(ext.Length - 3, 3);
            if (File.Exists(@"uploads\\" + civil_case.Text + "\\" + civil_case.Text + "." + ext))
            {
                MessageBox.Show("exists");
                File.Delete(@"uploads\\" + civil_case.Text + "\\" + civil_case.Text + "." + ext);
                File.Copy(upload_label1.Text, @"uploads\\" + civil_case.Text + "\\" + civil_case.Text + "." + ext, true);
            }
            else
            {
                File.Copy(upload_label1.Text, @"uploads\\" + civil_case.Text + "\\" + civil_case.Text + "." + ext, true);
            }
        }
        catch (Exception eeee)
        {
            MessageBox.Show(eeee + "");
        }
        try
        {
            string ext1 = upload_label2.Text;
            ext1 = ext1.Substring(ext1.Length - 3, 3);
            if (File.Exists(@"uploads\\" + civil_case.Text + "\\" + civil_case.Text + "_part2." + ext1))
            {
                MessageBox.Show("exists2");
                File.Delete(@"uploads\\" + civil_case.Text + "\\" + civil_case.Text + "." + ext1);
                File.Copy(upload_label2.Text, @"uploads\\" + civil_case.Text + "\\" + civil_case.Text + "_part2." + ext1, true);
            }
            else
            {
                File.Copy(upload_label2.Text, @"uploads\\" + civil_case.Text + "\\" + civil_case.Text + "_part2." + ext1, true);
            }
        }
        catch (Exception eeee1)
        {
            MessageBox.Show(eeee1 + "");
        }

        MessageBox.Show("Updated!");
        }

1 个答案:

答案 0 :(得分:0)

我没有看到两个上传按钮事件中的任何问题,但是在您的更新按钮中,第二个try / catch块中存在错误:

File.Delete(@"uploads\\" + civil_case.Text + "\\" + civil_case.Text + "." + ext1);

在第一个块中,您在四个位置构建相同的字符串:

@"uploads\\" + civil_case.Text + "\\" + civil_case.Text + "." + ext1

在第二个块中,您在三个位置构建此字符串,但错过第四个:

@"uploads\\" + civil_case.Text + "\\" + civil_case.Text + "_part2." + ext1

所以,你的第二个try / catch是从第一个文件中删除文件,所以只有第二个文件保留在最后。这与你观察到的相符吗?

此外,由于您提到缺乏C#/ OOP体验,这里有一个更好的方法来打破Upload Buttom代码(作为示例):

/// <summary>
/// Show a File Dialog and update a label if the dialog returns a file name.
/// </summary>
/// <param name="label">The label to update.</param>
private void ShowFileDialogAndUpdateLabel(Label label)
{
    var file = new OpenFileDialog
    {
        Title = "Browse the File",
        Filter = "PDF Files (*.PDF)|*.PDF|Images (*.BMP;*.JPEG;*.JPG;*.GIF;*.PNG;*.TIFF)|*.BMP;*.JPEG;*.JPG;*.GIF;*.PNG;*.TIFF|All files (*.*)|*.*"
    };

    if (file.ShowDialog() == DialogResult.OK)
        label.Text = Path.GetFullPath(file.FileName);
}

private void upload_1_Click(object sender, EventArgs e)
{
    ShowFileDialogAndUpdateLabel(upload_label1);
}

private void upload_2_Click(object sender, EventArgs e)
{
    ShowFileDialogAndUpdateLabel(upload_label2);
}