Search and Replace string in file in C# - c#

I am trying to create a small form in C# to find one string in a TMX file (xml) and replace it for another. Then it would create an output file with all the modifications.
The form contains a search button to locate the file in the local disk and a REPLACE button which it would change "srclang="all"" for "srclang="en-US"".
So far I have the following:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
using (FileDialog fileDialog = new OpenFileDialog())
{
if (DialogResult.OK == fileDialog.ShowDialog())
{
string filename = fileDialog.FileName;
textBox1.Text = fileDialog.FileName;
}
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
StreamWriter writer = null;
Dictionary<string, string> replacements = new Dictionary<string, string>();
replacements.Add("*all*", "en-US");
// ... further replacement entries ...
using (writer = File.CreateText("output.txt"))
{
foreach (string line in File.ReadLines(textBox1.Text))
{
bool replacementMade = false;
foreach (var replacement in replacements)
{
if (line.StartsWith(replacement.Key))
{
writer.WriteLine(string.Format("{1}",
replacement.Key, replacement.Value));
replacementMade = true;
break;
}
}
if (!replacementMade)
{
writer.WriteLine(line);
}
}
}
File.Replace("output.txt", textBox1.Text, "ORIGINAL_TMX_FILE.bak");
}
}
}
This code is from Dave R. from this site, it really works with TXT files but I am not sure what I am doing wrong. I am totally a newbie here.
If anyone could help me write some lines to make it work, I would really appreciate it!

Probably the simplest thing that could work would be to use these methods
File.WriteAllLines()
File.ReadAllLines()
as in the example below:
File.WriteAllLines(
outputFileName,
File.ReadAllLines(inputFileName)
.Select(line => line.Replace(#"srclang=""en-US""", #"srclang=""all"""));
If there could be whitespace in the search text, you could replace the call to string.Replace with a Regex.Replace.

I think I understand what you're trying to do and the following segment is the essence of your code:
if (line.StartsWith(replacement.Key))
{
writer.WriteLine(string.Format("{1}",
replacement.Key, replacement.Value));
replacementMade = true;
break;
}
If your dictionary will always have one element, you only have to change the parameter of the WriteLine call. You're writing only the replacement value instead of the whole line with the replacements done on it. You might want to have something like
writer.WriteLine(line.Replace(replacement.Key, replacement.Value)
If you want to check for several replacements, store the replaced line in the inner loop and only write it at the end and remove the break command.
Another unrelated observation: the replacement.Key parameter in the WriteLine call is omitted since {1} refers to the second additional parameter (counting starts from 0).

Related

C# - How to load text from text file in a listbox into a richTextBox?

Now solved. Thanks for your answers!
This is my code right now:
//Listbox scripts is the name of my folder
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + #"Listbox scripts"))
{
string file2 = file.Split('\\').Last();
listBox1.Items.Add(file2);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("SetText", new object[]
{
File.ReadAllText(string.Format("./Listbox scripts/{0}", listBox1.SelectedItem.ToString()))
});
}
I'm new to coding in C# and I have a textbox that has the names of text files in a directory and when I click on the text file in the listbox it's supposed to load the text from it into my textbox (named 'ScriptBox')
Here's my code:
private void Form1_Load(object sender, EventArgs e)
{
string User = System.Environment.MachineName;
textBox1.Text = "{CONSOLE} Welcome to Linst, " + User + "!";
directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + #"Scripts");
files = directory.GetFiles("*.txt");
foreach (FileInfo file in files)
{
listBox1.Items.Add(file.Name);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName); //these parts are the parts that dont work
}
Thanks in advance!
Add the below into your Form1.cs. What this is going to do is when a user clicks a listbox item, its going to call (raise an event) the "listBox1_MouseClick" method and set the text of the textbox to the text of the listbox item. I just quickly created an app and implemented the below and it works.
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.Text;
}
And add the below to the Form1.Designer.cs where the rest of your list box properties are. The below is subscribing to an event, the listBox1_MouseClick method in Form1.cs, so when a user clicks on a listbox item, the listBox1_MouseClick method is going to run.
this.listBox1.MouseClick += new MouseEventHandler(this.listBox1_MouseClick);
I hope the above makes sense.
Your code is nice, and perfect but it just need a little validation check in list index selection
Try thing in your listbox_selectedIndexChanged
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex!=-1)
{
FileInfo selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName);
}
}
I actually don't see a problem with your code, could it be a typo somewhere?
I did this and it worked for me:
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in System.IO.Directory.GetFiles(#"c:\"))
{
listBox1.Items.Add(file);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
textBox1.Text = System.IO.File.ReadAllText(listBox1.SelectedItem.ToString());
}
}

Calling an SQL stored procedure that's defined in a method using button click on form c#

Having a lot of trouble with this. I'm working on a large project, so there's only a few classes I'm interested in and working on. Basically, these are forms - one is a main editor where a user edits details and the other is used to assign a pin number. In the main editor form, if the user has a pin, they can choose to edit this pin. Here's where my problem lies - if I edit the pin, what I'm doing in the code is deleting the old pin and adding the new one. However, the database doesn't update until AFTER the editor form is closed. Therefore, I'd like to call the method that does change the database on the OKButton click, if I could. The problem I'm facing is I don't know how.
Here is the DB code, we'll say the class is called DetailsConn:
public string editPin(int driverID)
{
if (SchemaChecker.PINAvailable())
{
string sql = "EditPIN";
using (SqlCommand cmd = new SqlCommand(sql, base.connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Remove("#nDriverID");
cmd.Parameters.AddWithValue("#nDriverID", driverID);
cmd.Parameters.Remove("#nPIN");
SqlParameter pinParameter = cmd.Parameters.Add("#nPIN", SqlDbType.Char);
pinParameter.Direction = ParameterDirection.Output;
pinParameter.Size = 32;
cmd.ExecuteNonQuery();
return pinParameter.Value.ToString();
}
}
return "";
}
Here's the code for my edit:
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.listViewDriverTags.SelectedItems.Count > 0)
{
ListViewItem lvi = this.listViewDriverTags.SelectedItems[0];
DriverTag driverTag = lvi.Tag as DriverTag;
else if (blahTag.blahType == 2)
{
buttonAssignPIN_Click(sender, e);
}
//message stuff and dialog boxes with localization info
if (dr == DialogResult.Yes)
{
this.listViewDriverTags.Items.Remove(lvi);
if (Tag.id != -1)
{
TagsToBeDeleted.Add(driverTag);
}
}
if (dr == DialogResult.No)
{
this.listViewTags.Items.Clear();
this.listViewTags.Items.Add(lvi);
}
}
}
Here's my buttonAssignPIN stuff:
private void buttonAssignPIN_Click(object sender, EventArgs e)
{
using (AssignPINForm form = new AssignPINForm())
{
if (form.ShowDialog(this) == DialogResult.OK)
{
DriverTag PIN = DriverTag.GetNewPIN(form.DriverTag);
ListViewItem lvi = this.listViewTags.Items.Add(PIN.driverTag);
lvi.SubItems.Add(this.TagTypes[PIN.TagType]);
lvi.Tag = PIN;
}
}
}
And finally, here's my AssignPINForm code:
public partial class AssignPINForm : Form
{
public AssignPINForm()
{
InitializeComponent();
this.buttonOK.Click += new EventHandler(buttonOK_Click);
this.buttonCancel.Click += new EventHandler(buttonCancel_Click);
this.buttonOK.Enabled = false;
this.textBoxPin.TextChanged += delegate(object sender, EventArgs e)
{
String pattern = #"^[0-9]{4,20}$";
Regex regex = new Regex(pattern);
buttonOK.Enabled = regex.IsMatch(textBoxPin.Text);
};
LoadStrings();
}
public void LoadStrings()
{
//stome stuff
}
public string DriverTag
{
get { return this.textBoxPin.Text; }
set { this.textBoxPin.Text = value; }
}
private void buttonOK_Click(object sender, EventArgs e)
{
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void AssignPINForm_Load(object sender, EventArgs e)
{
}
}
I know it's kind of all over the place but I've provided everything I think is relevant. The middle two snippets are in the same class too, and the DB stuff is the same solution but a different project. I'd be grateful if someone can decipher what I'm after and help me out, it's the only thing I have left to do on this particular bit!
Thanks!
Not sure I fully got what you're after and I agree with some of the comments that this isn't the best of practice but I guess what you're after is to update the buttonOK_Click method to something like this:
private void buttonOK_Click(object sender, EventArgs e)
{
using(DetailsConn connection = new DetailsConn())
{
int driver = -1;
if(int.TryParse(this.DriverTag, out driver)) {
connection.editPin(driver);
}
}
}
Also, you may want to remove any other possible references to the editPin() function.
I actually figured out that even if I got that working correctly, it wasn't going to solve my problem. I've had to call a new procedure and declare that in the database schema - basically it was a lot more complicated than what I was giving it credit for. Thanks for the responses nonetheless.

break a text file into parts/lines and place the parts into text boxes

Right now my code takes the entire text file and just places it all into one text box. What I am trying to figure out how to do is have it place each line of the file into each separate text box.
namespace HomeInventory2
{
public partial class Form1 : Form
{
public Form1(string prepopulated)
{
InitializeComponent();
textBoxAmount.Text = prepopulated;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void submitButton_Click(object sender, EventArgs e)
{
CreateInventory create = new CreateInventory();
create.ItemAmount = textBoxAmount.Text;
create.ItemCategory = textBoxCategories.Text;
create.ItemProperties = textBoxValue.Text;
create.ItemValue = textBoxValue.Text;
InventoryMngr invtryMngr = new InventoryMngr();
invtryMngr.Create(create);
}
}
Assuming that the order of the lines is always the same and that each TextBox belongs to a line:
IEnumerable<String> lines = File.ReadLines(path);
textBoxAmount.Text = lines.ElementAtOrDefault(0);
textBoxCategories.Text = lines.ElementAtOrDefault(1);
textBoxValue.Text = lines.ElementAtOrDefault(2);
...
Enumerable.ElementAtOrDefault<TSource> Method
Returns the element at a specified index in a sequence or a default
value if the index is out of range (null in this case).
You could use System.IO.File.ReadAllLines(string filename).
What this does is reads each line of the file into a String array.
You could then do something like:
using System.IO;
//Namespace, Class Blah Blah BLah
String[] FileLines = File.ReadAllLines("Kablooey");
textBox1.Text = FileLines[0];
textbox2.Text = FileLines[1];
And so on. I hope this helps :)

How to create a large file with junk data with specified size more efficiently?

What I want to do is to write a program that can generate large files (1GB and up) filled with nothing less than random junk.
The problem is that if I try to create a file larger than 100MB my app uses more than 500MB Ram and takes for ever.
Steps I'm using:
use loop to generate junk data and add it to character array.
store all characters from the array to the variable.
save the data from variable to the file.
Is there any better solution for this? Maybe some shortcut?
This is what I'm working with:
namespace Fake_File_Creator
{
public partial class MainWindow : Form
{
private string text;
private List<char> stringChars;
public MainWindow()
{
InitializeComponent();
}
private void btnNewFile_Click(object sender, EventArgs e)
{
sfdNewFile.Filter = "All Files|*.*";
if (sfdNewFile.ShowDialog() == DialogResult.OK)
{
lblMessage.Text = "Generating data...";
bwCreateData.RunWorkerAsync((int)nudSize.Value * 1024000);
}
}
private void bwCreateData_DoWork(object sender, DoWorkEventArgs e)
{
var random = new Random();
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 ~!##$%^&*()_+ /.,'[];{}|:<>?";
stringChars = new List<char>();
for (int i = 0; i < (int)e.Argument; i++)
{
stringChars.Add(chars[random.Next(chars.Length)]);
}
text = new string(stringChars.ToArray());
}
void bwCreateData_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
lblMessage.Text = "Saving file...";
bwCreateFile.RunWorkerAsync();
}
private void bwCreateFile_DoWork(object sender, DoWorkEventArgs e)
{
using (StreamWriter outfile = new StreamWriter(sfdNewFile.FileName))
{
outfile.Write(text);
}
}
void bwCreateFile_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
lblMessage.Text = "File succesfully created...";
text = string.Empty;
stringChars.Clear();
stringChars = null;
GC.Collect();
}
}
}
Just write a ton of data to the file directly rather than saving to memory in the string "text".
You can call outfile.Write("junk text") in a loop.
It might help to preallocate the List via
stringChars = new List<char>((int)e.Argument);
Is the set of characters important? You might try instead generating a random number between 1 and 94 (or so), add 31 and convert to ASCII char. You'll get most of the printable ascii set that way and save lookups in the chars array.
EDIT... I agree with the previous poster (James Cotter) -- why write to your temporary array when you can just do
streamWriter.Write(randomCharacter)

Grouping two methods together in C#

I currently have two different event handlers in C#, which perform two different functions. Although how could I combine the two methods together, so only 1 button could perform both actions? (Taking into account that button1_Click event must be performed first.)
private void button2_Click(object sender, EventArgs e)
{
var file = File.AppendText(#"c:\output2.txt");
foreach (string tmpLine in File.ReadAllLines(#"c:\output.txt"))
{
if (File.Exists(tmpLine))
{
file.WriteLine(tmpLine);
}
}
file.Close();
}
private void button1_Click(object sender, EventArgs e)
{
using (StreamWriter sw = File.AppendText(#"c:\output.txt"))
{
StreamReader sr = new StreamReader(#"c:\filename.txt");
string myString = "";
while (!sr.EndOfStream)
{
myString = sr.ReadLine();
int index = myString.LastIndexOf(":");
if (index > 0)
myString = myString.Substring(0, index);
sw.WriteLine(myString);
}
button2_Click(sender, e);
}
}
Instead of writing the code in the event handler, bring them out into two functions and then call those functions whichever way you want from the event handler.
You could, if I'm understanding you correctly, have one event handler call another. An event handler is "just" a method after all, so:
private void button1_Click(object sender, EventArgs e)
{
// All the code that's currently there
button2_Click(sender, e);
}
Or, you could extract the code from the event handlers into separate methods:
private void button1_Click(object sender, EventArgs e)
{
WriteToOutputDotTxt();
OtherMethodThatWritesToOutputDotTxt();
}
private void button2_Click(object sender, EventArgs e)
{
OtherMethodThatWritesToOutputDotTxt();
}
private void WriteToOutputDotTxt()
{
// Code that's currently in button1_Click
}
private void OtherMethodThatWritesToOutputDotTxt()
{
// Code that's currently in button2_Click
}
The code doesn't have to be contained within the event handlers, in fact you'll find it easier (if you're interested!) to test your code if you can separate it away from your UI. You could, for example, have a class called ProcessOutputFile and move the WriteToOutputDotTxt and OtherMethodThatWritesToOutputDotTxt methods onto that class. It's then a lot easier to write tests for that code as it's not "tied into" the UI code.
I try to keep most logic out of event handlers and create functions with logical names that i call from the event handlers. Then they can be called from wherever.
Just attach two event handlers to some button:
somebutton.Click += new EventHandler(button1_Click);
somebutton.Click += new EventHandler(button2_Click);
// given these two methods extracted from your events
void DoBar(object sender, EventArgs e)
{
var file = File.AppendText(#"c:\output.txt");
foreach (string tmpLine in File.ReadAllLines(#"c:\filename.txt"))
{
if (File.Exists(tmpLine))
{
file.WriteLine(tmpLine);
}
}
file.Close();
}
void DoFoo(object sender, EventArgs e)
{
using (StreamWriter sw = File.AppendText(#"c:\output.txt"))
{
StreamReader sr = new StreamReader(#"c:\filename.txt");
string myString = "";
while (!sr.EndOfStream)
{
myString = sr.ReadLine();
int index = myString.LastIndexOf(":");
if (index > 0)
myString = myString.Substring(0, index);
sw.WriteLine(myString);
}
}
}
// you can subscribe like this
button1.Click += DoFoo;
button1.Click += DoBar;
button2.Click += DoBar;
EDIT
forgot my sender and eventargs

Categories