So I have a bit complicated one, I'm trying to create a "template creator". User will input data via comboboxes and textboxes into a form, from which a button generates the names (combination of inputs). After that next button creates directories as required. Until this point everything is ok, however, after this, I prompted a question whether the user wants to start copying files to the newly created directories.
Current code:
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();
}
}
}
}
As you can see I've set the targetPathAssy
to the same location as what the new folder is created, but I'm not sure if the code can read that? Or how could I store that newly created directory path and call it?
I found the solution, and it's much simpler than what I've tried before.
Here's the full code:
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;
}
}
}
The change:
Turns out that I can use the same string to copy the file to the newly created directory which uses the string dirPath
as shown above. Although I've embedded the files into the application (ease of use, not sure if I would get a dedicated folder on the server) I've used the
File.Copy(@"Resources\UNIT_START_PRODUCT.CATProduct", targetPathAssy + @"\" + "UNIT_START_PRODUCT.CATProduct");
I had to add the @"\"
because otherwise the copied file would have the targetPathAssy
added to its name instead of going into the specific folder.
Hope this helps someone in the future