Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C替换目录中存在的文件#_C#_File Upload - Fatal编程技术网

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

C# 使用C替换目录中存在的文件#,c#,file-upload,C#,File Upload,我想在C#Gui中上传一个文件,并检查它是否已经存在于该特定文件夹中。但为什么只有第二个文件被替换,即使第一个文件已经存在也不替换?我有两个上传按钮 这两个按钮 private void upload_1_Click(object sender, EventArgs e) { OpenFileDialog file = new OpenFileDialog(); file.Title = "Browse the File"; fil

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

这两个按钮

    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!");
        }

.

我在两个Upload Button事件中没有发现任何问题,但是在您的Update Button中,第二个try/catch块中有一个bug:

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代码(作为示例):

//
///如果对话框返回文件名,则显示文件对话框并更新标签。
/// 
///要更新的标签。
私有void显示文件对话框和更新标签(标签标签)
{
var file=新建OpenFileDialog
{
Title=“浏览文件”,
Filter=“PDF文件(*.PDF)|*.PDF |图像(*.BMP;*.JPEG;*.JPG;*.GIF;*.PNG;*.TIFF)|*.BMP;*.JPEG;*.JPG;*.GIF;*.PNG;*.TIFF |所有文件(*.*)|*”
};
if(file.ShowDialog()==DialogResult.OK)
label.Text=Path.GetFullPath(file.FileName);
}
私有无效上载\u 1\u单击(对象发送者,事件参数e)
{
ShowFileDialogAndUpdateLabel(上传标签1);
}
私有无效上载\u 2\u单击(对象发送者,事件参数e)
{
ShowFileDialogAndUpdateLabel(上传标签2);
}

面向对象编程的全部目的是能够一次性编写代码并重用代码。但我对C#还是新手,我对oop的工作原理一无所知。这就是为什么我可以在这里找到多余的代码。再次抱歉。@ USE356964:这两个文件的文件扩展是相同的还是不同的?它可以是相同的或不同的……你会考虑创建一个新的变量来存储文件路径,这样你就不需要使用两组相同的代码吗?
/// <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);
}