将文件复制到新创建的目录
Posted
技术标签:
【中文标题】将文件复制到新创建的目录【英文标题】:Copy file to newly created directories 【发布时间】:2021-11-27 06:49:01 【问题描述】:所以我有一个有点复杂的,我正在尝试创建一个“模板创建者”。用户将通过组合框和文本框将数据输入到表单中,按钮从中生成名称(输入组合)。之后,下一个按钮根据需要创建目录。到此为止一切正常,但是,在此之后,我提示了一个问题,用户是否要开始。
当前代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Microsoft.VisualBasic;
namespace ME_Nitra_BIW_App
public partial class ToolingDesign : Form
// create folder path to enable new folder creation
private void btnGenerateFilePath_Click(object sender, EventArgs e)
folderBrowserDialog1.ShowDialog();
tBoxFilePath.Text = folderBrowserDialog1.SelectedPath;
btnCreateDir.Enabled = true;
private void btnCreateDir_Click(object sender, EventArgs e)
if (String.IsNullOrEmpty(tBoxFilePath.Text))
System.Windows.Forms.MessageBox.Show("No file path selected!");
else
// for Assembly folder
string fileNameAssy = "FIXED_PARTS_PRODUCT.CATProduct";
string sourcePathAssy = @"c:\Users\mjanus\Downloads\CATIAV5\START_MODELS\CAT_PRODUCTS";
string targetPathAssy = tBoxFilePath.Text + @"\" + tBoxFolderName.Text + @"\" + tBoxFolderName.Text + "_U000" + "_ASSEMBLY";
// use path class to manipulate file and directory paths
string sourceFile = System.IO.Path.Combine(sourcePathAssy, fileNameAssy);
string destFile = System.IO.Path.Combine(targetPathAssy, fileNameAssy);
string dirPath = tBoxFilePath.Text + @"\" + tBoxFolderName.Text + @"\" + tBoxFolderName.Text;
// create new folders with generated names
btnGenerateFilePath.Enabled = false;
btnCreateDir.Enabled = false;
Directory.CreateDirectory(tBoxFilePath.Text + @"\" + tBoxFolderName.Text);
System.Threading.Thread.Sleep(500);
Directory.CreateDirectory(dirPath + "_U000" + "_ASSEMBLY");
Directory.CreateDirectory(dirPath + "_U001" + "_PRODUCT_PARTS");
Directory.CreateDirectory(dirPath + "_U002" + "_CLAMP_STUDY");
Directory.CreateDirectory(dirPath + "_U003" + "_GUN_STUDY");
Directory.CreateDirectory(dirPath + "_U004" + "_PRODUCT_PARTS");
Directory.CreateDirectory(dirPath + "_U005" + "_MECHANICAL_SEQUENCES");
Directory.CreateDirectory(dirPath + "_U006" + "_MISCELLANEOUS");
Directory.CreateDirectory(dirPath + "_U007" + "_SUPPORT");
// ask if user wants to copy template files to the newly created folders
DialogResult dialogResult = MessageBox.Show("Directories successfuly created!" + Environment.NewLine + "Do you wish to copy files now?", "Success!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
// if the directory folder already exists, this method does not create a new directory
System.IO.Directory.CreateDirectory(targetPathAssy);
// overwrite the destination file if it already exists
System.IO.File.Copy(sourceFile, destFile, true);
// start of copy
if (System.IO.Directory.Exists(sourcePathAssy))
string[] files = System.IO.Directory.GetFiles(sourcePathAssy);
foreach (string s in files)
fileNameAssy = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPathAssy, fileNameAssy);
System.IO.File.Copy(s, destFile, true);
else
MessageBox.Show("Source path does not exist!");
else if (dialogResult == DialogResult.No)
this.Close();
如您所见,我已将targetPathAssy
设置为与创建新文件夹相同的位置,但我不确定代码是否可以读取?或者我如何存储新创建的目录路径并调用它?
【问题讨论】:
使用Path.Combine
来构建路径,而不是字符串连接。并且,为了创建真正可读的代码,请在放置控件后重命名控件表单。充满 Textbox6、Button23、Label19 的代码实际上是被混淆的垃圾。你可以很奢侈地回到设计器中,检查名字是 textBox34 还是 textBox38,但我们没有……而且想一想,如果它被称为 firstNameTextBox,您甚至不必重新进入设计器
" 我不确定代码是否可以读取?" 你测试了吗?您是否使用调试器检查了变量的值?实际问题是什么?还是这只是一种直觉?
我在理解您的问题时遇到了问题。您将变量targetPathAssy
设置为某个值。用于在下面几行创建目录的值相同。稍后,您尝试再次创建相同的目录,这次使用变量targetPathAssy
。我不确定这是否是您在问题中提到的问题,但此时代码似乎没有意义。
我认为我可以重复使用相同的目录路径,这就是为什么问这是否可以工作,基本上在创建新目录之后我想将某些文件复制到那些新目录中我得到了mscorlib.dll 中出现“System.UnauthorizedAccessException”,附加信息:对路径“C:\Users\mjanus\Desktop\Test\L462_BR02_010_011_COM_TR\L462_BR02_010_011_COM_TR_U000_ASSEMBLY\FIXED_PARTS_PRODUCT.CATProduct”的访问被拒绝。
假设您创建了一个基本路径来简化事情并使其更具可读性:var templatePath = tBoxFilePath.Text + @"\" + tBoxFolderName.Text + @"\" ;
。然后在您的代码中使用templatePath
而不是tBoxFilePath.Text + @"\" + tBoxFolderName.Text
。另外,我注意到您使用它的次数太多了,例如tBoxFilePath.Text + @"\" + tBoxFolderName.Text + @"\" + tBoxFolderName.Text + "_U000" + "_ASSEMBLY"
:如果你之前声明了var fileNameAssy = ...
,不应该像templatePath + fileNameAssy
吗?使代码更易于理解且不易出错。
【参考方案1】:
我找到了解决方案,而且比我之前尝试过的要简单得多。
这是完整的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Microsoft.VisualBasic;
namespace ME_Nitra_BIW_App
public partial class ToolingDesign : Form
public ToolingDesign()
InitializeComponent();
radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
private void btnCreateDir_Click(object sender, EventArgs e)
if (String.IsNullOrEmpty(tBoxFilePath.Text))
System.Windows.Forms.MessageBox.Show("No file path selected!");
else
// directory path
string dirPath = tBoxFilePath.Text + @"\" + tBoxFolderName.Text + @"\" + tBoxFolderName.Text;
// where to paste the files
string targetPathAssy = dirPath + "_U000" + "_ASSEMBLY";
// create new folders with generated names
btnGenerateFilePath.Enabled = false;
btnCreateDir.Enabled = false;
Directory.CreateDirectory(tBoxFilePath.Text + @"\" + tBoxFolderName.Text);
System.Threading.Thread.Sleep(500);
Directory.CreateDirectory(dirPath + "_U000" + "_ASSEMBLY");
Directory.CreateDirectory(dirPath + "_U001" + "_PRODUCT_PARTS");
Directory.CreateDirectory(dirPath + "_U002" + "_CLAMP_STUDY");
Directory.CreateDirectory(dirPath + "_U003" + "_GUN_STUDY");
Directory.CreateDirectory(dirPath + "_U004" + "_PRODUCT_PARTS");
Directory.CreateDirectory(dirPath + "_U005" + "_MECHANICAL_SEQUENCES");
Directory.CreateDirectory(dirPath + "_U006" + "_MISCELLANEOUS");
Directory.CreateDirectory(dirPath + "_U007" + "_SUPPORT");
// ask if user wants to copy template files to the newly created folders
DialogResult dialogResult = MessageBox.Show("Directories successfuly created!" + Environment.NewLine + "Do you wish to copy files now?", "Success!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
// test copy
File.Copy(@"Resources\UNIT_START_PRODUCT.CATProduct", targetPathAssy + @"\" + "UNIT_START_PRODUCT.CATProduct");
else if (dialogResult == DialogResult.No)
this.Close();
// create folder path to enable new folder creation
private void btnGenerateFilePath_Click(object sender, EventArgs e)
folderBrowserDialog1.ShowDialog();
tBoxFilePath.Text = folderBrowserDialog1.SelectedPath;
btnCreateDir.Enabled = true;
变化:
原来我可以使用相同的字符串将文件复制到新创建的目录,该目录使用字符串dirPath
,如上所示。虽然我已经将文件嵌入到应用程序中(易于使用,不确定我是否会在服务器上获得一个专用文件夹)我已经使用了
File.Copy(@"Resources\UNIT_START_PRODUCT.CATProduct", targetPathAssy + @"\" + "UNIT_START_PRODUCT.CATProduct");
我必须添加@"\"
,否则复制的文件会在其名称中添加targetPathAssy
,而不是进入特定文件夹。
希望这对未来的人有所帮助
【讨论】:
以上是关于将文件复制到新创建的目录的主要内容,如果未能解决你的问题,请参考以下文章