Цитата Futtoto:
покажите пожалуйста пример кода.
|
Iska Вам озвучил общий алгоритм действий, так что показывать там особо нечего. Но если Вам непременно нужен код, то в самом простом виде это будет выглядеть так:
Код:

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Text.RegularExpressions;
[assembly: AssemblyTitle("Replacing")]
namespace Replacing {
internal sealed class AssemblyInfo {
private Type a;
internal AssemblyInfo() { a = typeof(Program); }
internal String Title {
get {
return ((AssemblyTitleAttribute)Attribute.GetCustomAttribute(
a.Assembly, typeof(AssemblyTitleAttribute)
)).Title;
}
}
}
internal sealed class frmMain : Form {
public frmMain() {
InitializeComponent();
this.Text = new AssemblyInfo().Title;
}
private String folder; //здесь хранится путь до выбранной папки
private Label lblLabel1;
private Label lblLabel2;
private TextBox txtBox1;
private TextBox txtBox2;
private Button btnButton1;
private Button btnButton2;
private void InitializeComponent() {
this.lblLabel1 = new Label();
this.lblLabel2 = new Label();
this.txtBox1 = new TextBox();
this.txtBox2 = new TextBox();
this.btnButton1 = new Button();
this.btnButton2 = new Button();
//
//lblLabel1
//
this.lblLabel1.Height = 13;
this.lblLabel1.Location = new Point(7, 7);
this.lblLabel1.Text = "Что ищем:";
//
//lblLabel2
//
this.lblLabel2.Height = 13;
this.lblLabel2.Location = new Point(7, 51);
this.lblLabel2.Text = "На что заменяем:";
//
//txtBox1
//
this.txtBox1.Location = new Point(7, 25);
this.txtBox1.Width = 313;
//
//txtBox2
//
this.txtBox2.Location = new Point(7, 69);
this.txtBox2.Width = 313;
//
//btnButton1
//
this.btnButton1.Location = new Point(33, 101);
this.btnButton1.Text = "Обзор...";
this.btnButton1.Click += (s, e) => {
using (FolderBrowserDialog fbd = new FolderBrowserDialog()) {
if (fbd.ShowDialog() == DialogResult.OK) {
folder = fbd.SelectedPath;
}
}
};
//
//btnButton2
//
this.btnButton2.Location = new Point(213, 101);
this.btnButton2.Text = "Заменить";
this.btnButton2.Click += (s, e) => {
if (String.IsNullOrEmpty(folder) ||
String.IsNullOrEmpty(txtBox1.Text) ||
String.IsNullOrEmpty(txtBox2.Text)) {
MessageBox.Show(
"Ничего не выбрано, идите лесом!", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error
);
return;
}
foreach (String file in Directory.EnumerateFiles(
folder, "*.txt", SearchOption.AllDirectories //ищем все txt файлы
)) {
String content = File.ReadAllText(file, Encoding.Default);
String n_content = Regex.Replace(content, txtBox1.Text, txtBox2.Text);
File.WriteAllText(file, n_content);
}
};
//
//frmMain
//
this.ClientSize = new Size(330, 170);
this.Controls.AddRange(new Control[] {
this.lblLabel1, this.lblLabel2, this.txtBox1,
this.txtBox2, this.btnButton1, this.btnButton2
});
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
}
}
internal sealed class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new frmMain());
}
}
}