C# 选择文件夹,遍历文件夹,复制图片并改名,读写XML

 
 
private void button2_Click(object sender, EventArgs e)
{
 
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("FilePath.xml");
XmlNodeList xn = xmlDoc.SelectSingleNode("Test").ChildNodes;
string strPath = string.Empty;
foreach (XmlNode item in xn)
{
if (item.Name.Equals("Path"))
strPath = item.InnerText.Trim();
}
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.SelectedPath = strPath;
dialog.Description = "请选择文件夹路径";
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == DialogResult.OK)
{
txtFile.Text = dialog.SelectedPath;
}
}
 
private void button3_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择存放路径";
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == DialogResult.OK)
{
txtEndFile.Text = dialog.SelectedPath;
}
}
 
private void button1_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(txtFile.Text))
{
MessageBox.Show("请选择路径!");
return;
}
if (string.IsNullOrEmpty(txtEndFile.Text))
{
MessageBox.Show("请选择存放路径!");
return;
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("FilePath.xml");
//XmlNode root = xmlDoc.SelectSingleNode("Test");//查找<Test>
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Test").ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.Name.Equals("Path"))//如果name值为“Path”
{
xe.InnerText = txtFile.Text;//则修改
break;
}
}
xmlDoc.Save("FilePath.xml");//保存。
button1.Enabled = false;
 
//C#遍历指定文件夹中的所有文件
DirectoryInfo TheFolder = new DirectoryInfo(txtFile.Text);
//遍历文件夹
//foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories())
//this.listBox1.Items.Add(NextFolder.Name);
//遍历文件
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
if (NextFile.Name.Length >= Convert.ToInt32(textBox1.Text))
File.Copy(txtFile.Text + "\\" + NextFile.Name, txtEndFile.Text + "\\" + NextFile.Name.Remove(Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox3.Text)));
else
File.Copy(txtFile.Text + "\\" + NextFile.Name, txtEndFile.Text + "\\" + NextFile.Name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
button1.Enabled = true;
MessageBox.Show("处理完成");
}
private void SetText()
{
try
{
if (!string.IsNullOrEmpty(textBox3.Text) && !string.IsNullOrEmpty(textBox2.Text) &&
!string.IsNullOrEmpty(textBox1.Text))
{
if (textBox5.Text.Length >= Convert.ToInt32(textBox1.Text))
textBox4.Text = textBox5.Text.Remove(Convert.ToInt32(textBox2.Text),
Convert.ToInt32(textBox3.Text));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
 
private void textBox3_TextChanged(object sender, EventArgs e)
{
SetText();
}
 
private void textBox2_TextChanged(object sender, EventArgs e)
{
SetText();
}
 
private void textBox1_TextChanged(object sender, EventArgs e)
{
SetText();
}
 
private void CopyPicForm_Load(object sender, EventArgs e)
{
SetText();
}

转载于:https://www.cnblogs.com/zishen/p/5896317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值