c#文件复制.文件类型

本文关键字:文件 类型 复制 | 更新日期: 2023-09-27 18:17:35

我想复制一个文件到这个应用程序的目录。唯一的问题是,它需要是相同的文件类型,因为它是在它来自的目录。在这段代码中,我必须把文件类型后面的名称。当我将文件复制到第二个目录时,它会自动变成.txt文件。我想要相同的扩展,因为它在第一个目录。我该怎么做呢?下面是我的代码:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Transfer_Click(object sender, EventArgs e)
    {
        File.Copy(@""+textBox1.Text, @""+textBox2.Text+"/"+ textBox3.Text);
        label2.Text = "File Transfer Succeeded";
    }
    private void Filesource_Click(object sender, EventArgs e)
    {
        DialogResult resDialog = openFileDialog1.ShowDialog();
        if (resDialog.ToString() == "OK")
        {
            textBox1.Text = openFileDialog1.FileName;
        }
    }
    private void Target_Click(object sender, EventArgs e)
    {
        DialogResult resDialog = folderBrowserDialog1.ShowDialog();
        if (resDialog.ToString() == "OK")
        {
            textBox2.Text = folderBrowserDialog1.SelectedPath;
        }
    }
}
}

c#文件复制.文件类型

我认为你想这样做:

private void Transfer_Click(object sender, EventArgs e)
    {
        File.Copy(textBox1.Text, Path.Combine(textBox2.Text, Path.ChangeExtension(textBox3.Text, Path.GetExtension(textBox1.Text)));
        label2.Text = "File Transfer Succeeded";
    }

希望这对你有帮助!

string sourcePath = @"c:'myDocument.docx";
            string targetPath = @"c:'xyzHello.crazyextension";
            string sourceExtension = Path.GetExtension(sourcePath);
            if (sourceExtension != Path.GetExtension(targetPath))
                targetPath = Path.ChangeExtension(targetPath, sourceExtension);
            File.Copy(sourcePath, targetPath);