I am trying to make a small program to duplicate one file "X" (user input) times. So i created this windows forms app
I am not sure how to write the code for the Start button where I can say File.Copy
the specific file 1000 times let's say and save them in the desired folder.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
tbAlegefisierul.Text = openFileDialog1.FileName;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void lbAlegeFisierul_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void btnSalveaza_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{tbSalveaza.Text = Path.Combine(folderBrowserDialog1.SelectedPath); }
}
private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
{
}
private void btnStart_Click(object sender, EventArgs e)
{
File.Copy(tbAlegefisierul.Text, tbSalveaza.Text, true);
MessageBox.Show("Ai copiat cu succes!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void tbnrori_TextChanged(object sender, EventArgs e)
{
}
}
CodePudding user response:
You probably need something like this:
private string _fileName = null;
private string _selectedPath = null;
private int? _nrori = null;
private void UpdateStartButton()
{
btnStart.Enabled =
File.Exists(_fileName)
&& Directory.Exists(_selectedPath)
&& _nrori.HasValue
&& _nrori.Value > 0;
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
tbAlegefisierul.Text = openFileDialog1.FileName;
_fileName = openFileDialog1.FileName;
this.UpdateStartButton();
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.UpdateStartButton();
}
private void btnSalveaza_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
tbSalveaza.Text = folderBrowserDialog1.SelectedPath;
_selectedPath = folderBrowserDialog1.SelectedPath;
this.UpdateStartButton();
}
}
private void btnStart_Click(object sender, EventArgs e)
{
for (int i = 0; i < _nrori.Value; i )
{
var destinationFileName = Path.Combine(_selectedPath, $"{Path.GetFileNameWithoutExtension(_fileName)} ({i 1}){Path.GetExtension(_fileName)}");
File.Copy(_fileName, destinationFileName, true);
}
MessageBox.Show("Ai copiat cu succes!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void tbnrori_TextChanged(object sender, EventArgs e)
{
_nrori = int.TryParse(tbnrori.Text, out int n) ? (int?)n : null;
this.UpdateStartButton();
}