C# - 创建具有特定名称的多个文件
C# - creating multiple files with specific names
我已经开始学习 C# 中的 System.IO
,我想实现这样的目标:
- 我有两个按钮和一个
TextBox
。
- 第一个按钮事件应该使用
FolderBrowserDialog
,让我选择一个特定的folder.Then,将其路径保存在变量中。
- 文本框 应该获取我要在所选路径中创建的文件夹数量的值。
- 第二个按钮将创建在第一个按钮的文本框中写入的文件夹数量(每个名称不同)指定路径。
到目前为止我的按钮和文本框事件:
...
int value;
String path;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog(this) == DialogResult.OK)
{
MessageBox.Show(fbd.SelectedPath);
path = folderBrowserDialog1.SelectedPath;//store selected path to variable "path"
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
value = Convert.ToInt32(textBox1.Text);//store the value from the textbox in variable "value"
}
private void button2_Click(object sender, EventArgs e)
{
if (!Directory.Exists(path))//if selected path exists
{
for(int i=0;i<value;i++)//trying to go through as folders as I wrote in the TextBox
{
Directory.CreateDirectory(path + "something");//is something wrong here, I guess.
}
}
}
我目前的问题:
- 我的代码有什么问题?
- 每次
for(){}
执行不同名称的文件夹时如何创建?
如有任何帮助,我将不胜感激
int value;
string path = null;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog(this) == DialogResult.OK)
{
MessageBox.Show(fbd.SelectedPath);
path = fbd.SelectedPath; //fbd not folderBrowserDialog1
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
value = Convert.ToInt32(textBox1.Text);//store the value from the textbox in variable "value"
}
private void button2_Click(object sender, EventArgs e)
{
if (path != null && Directory.Exists(path))
for(int i=0;i<value;i++)
Directory.CreateDirectory(Path.Combine(path,string.Format("SomeFolder{0}",i)));
}
我已经开始学习 C# 中的 System.IO
,我想实现这样的目标:
- 我有两个按钮和一个
TextBox
。 - 第一个按钮事件应该使用
FolderBrowserDialog
,让我选择一个特定的folder.Then,将其路径保存在变量中。 - 文本框 应该获取我要在所选路径中创建的文件夹数量的值。
- 第二个按钮将创建在第一个按钮的文本框中写入的文件夹数量(每个名称不同)指定路径。
到目前为止我的按钮和文本框事件:
...
int value;
String path;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog(this) == DialogResult.OK)
{
MessageBox.Show(fbd.SelectedPath);
path = folderBrowserDialog1.SelectedPath;//store selected path to variable "path"
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
value = Convert.ToInt32(textBox1.Text);//store the value from the textbox in variable "value"
}
private void button2_Click(object sender, EventArgs e)
{
if (!Directory.Exists(path))//if selected path exists
{
for(int i=0;i<value;i++)//trying to go through as folders as I wrote in the TextBox
{
Directory.CreateDirectory(path + "something");//is something wrong here, I guess.
}
}
}
我目前的问题:
- 我的代码有什么问题?
- 每次
for(){}
执行不同名称的文件夹时如何创建?
如有任何帮助,我将不胜感激
int value;
string path = null;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog(this) == DialogResult.OK)
{
MessageBox.Show(fbd.SelectedPath);
path = fbd.SelectedPath; //fbd not folderBrowserDialog1
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
value = Convert.ToInt32(textBox1.Text);//store the value from the textbox in variable "value"
}
private void button2_Click(object sender, EventArgs e)
{
if (path != null && Directory.Exists(path))
for(int i=0;i<value;i++)
Directory.CreateDirectory(Path.Combine(path,string.Format("SomeFolder{0}",i)));
}