C# 如何制作OpenFileDialog控件中所选文件的副本
在上面的代码中,我浏览要打开的文件。我要做的是,浏览一个文件,选择它,然后按ok。单击“确定”后,我想复制所选文件,并为该重复文件提供.txt扩展名。我需要帮助来实现这一目标C# 如何制作OpenFileDialog控件中所选文件的副本,c#,winforms,openfiledialog,C#,Winforms,Openfiledialog,在上面的代码中,我浏览要打开的文件。我要做的是,浏览一个文件,选择它,然后按ok。单击“确定”后,我想复制所选文件,并为该重复文件提供.txt扩展名。我需要帮助来实现这一目标 谢谢您使用了文件。请按如下方式复制 // Browses file with OpenFileDialog control private void btnFileOpen_Click(object sender, EventArgs e) { OpenFileDialog openFil
谢谢您使用了文件。请按如下方式复制
// Browses file with OpenFileDialog control
private void btnFileOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialogCSV = new OpenFileDialog();
openFileDialogCSV.InitialDirectory = Application.ExecutablePath.ToString();
openFileDialogCSV.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialogCSV.FilterIndex = 1;
openFileDialogCSV.RestoreDirectory = true;
if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
{
this.txtFileToImport.Text = openFileDialogCSV.FileName.ToString();
}
}
试试这个
File.Copy(openFileDialogCSV.FileName., openFileDialogCSV.FileName + ".txt");
123可以通过您想要的任何文件名进行更改
private void btnFileOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialogCSV = new OpenFileDialog();
openFileDialogCSV.InitialDirectory = Application.ExecutablePath.ToString();
openFileDialogCSV.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialogCSV.FilterIndex = 1;
openFileDialogCSV.RestoreDirectory = true;
if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
{
this.txtFileToImport.Text = openFileDialogCSV.FileName.ToString();
System.IO.File.Copy(this.txtFileToImport.Text,"C://123.txt")
}
}
上述代码将所选文件复制为同名的txt文件,并将其复制到同一目录中
如果需要用相同的名称覆盖现有文件,请将另一个参数添加到复制方法中,使其为true
if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
{
var fileName = openFileDialogCSV.FileName;
System.IO.File.Copy( fileName ,Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName)+".txt"));
}