将文件复制到新创建的目录
Copy file to newly created directories
所以我有点复杂,我正在尝试创建一个“模板创建器”。用户将通过组合框和文本框将数据输入到表单中,按钮从中生成名称(输入组合)。之后,下一个按钮根据需要创建目录。至此一切正常,然而,此后,我提示用户是否要开始将文件复制到新创建的目录中。
当前代码:
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
设置为与创建新文件夹相同的位置,但我不确定代码是否可以读取它?或者我如何存储新创建的目录路径并调用它?
我找到了解决方案,它比我以前尝试过的要简单得多。
完整代码如下:
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
,而不是进入特定文件夹。
希望这对以后的人有所帮助
所以我有点复杂,我正在尝试创建一个“模板创建器”。用户将通过组合框和文本框将数据输入到表单中,按钮从中生成名称(输入组合)。之后,下一个按钮根据需要创建目录。至此一切正常,然而,此后,我提示用户是否要开始将文件复制到新创建的目录中。
当前代码:
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
设置为与创建新文件夹相同的位置,但我不确定代码是否可以读取它?或者我如何存储新创建的目录路径并调用它?
我找到了解决方案,它比我以前尝试过的要简单得多。
完整代码如下:
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
,而不是进入特定文件夹。
希望这对以后的人有所帮助