I'm trying to replace a certain line in a .txt file when I click the Update Button
This is what my program looks like
http://i.imgur.com/HKu4bGo.png
This is my code so far
string[] arrLine = File.ReadAllLines("Z:/Daniel/SortedAccounts.txt");
arrLine[accountComboBox.SelectedIndex] = "#1#" + firstNameInfoBox.Text + "#2#" + lastNameInfoBox.Text + "#3#" + emailInfoBox.Text + "#4#" + phoneNumberInfoBox.Text + "#5#EMAIL#6#";
File.WriteAllLines("Z:/Daniel/SortedAccounts.txt", arrLine);
This is what's inside SortedAccounts.txt
#1#Bob#2#Smith#3#Bob#Smith.com#4#5551234567#5#EMAIL#6#
#1#Dan#2#Lastyy#3#Daniel#Lastyy.com#4#5551234567#5#EMAIL#6#
The ComboBox is in the order as the Txt File.
So I get the same Index as the selected item in the ComboBox. And then I want to delete that line and then add a new line that same txt file with the updated information.
My code isn't doing this for some reason though and I can't figure it out
Try this out using List to easily remove an entry at a certain index. Don't forget to reload the combobox data source when the file is updated to avoid index mismatch etc..
List<string> arrLine = File.ReadAllLines("Z:/Daniel/SortedAccounts.txt").ToList();
arrLine.RemoveAt(accountComboBox.SelectedIndex);
string newLine = "#1#" + firstNameInfoBox.Text + "#2#" + lastNameInfoBox.Text + "#3#" + emailInfoBox.Text + "#4#" + phoneNumberInfoBox.Text + "#5#EMAIL#6#";
arrLine.Add(newLine);
File.WriteAllLines("Z:/Daniel/SortedAccounts.txt", arrLine);
Related
I have a loop, which writes the values of an array into a .csv file. It is appending each line, so it writes the values vertically, however, I would like it to write each value in a different column rather than by line, that way I can filter the content after running the program.
My initial thought was to save all the values in one variable and then just write the variable to the .csv file, but I believe this would fill all values into one cell instead of distributing them to different columns.
I need it to write all of the values of the array on each loop, and then move to the next line on the each time it loops if that makes sense.
string pathCleansed = #"myfilename.csv";
string[] createText = {
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].StreetAddress1,
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].StreetAddress2,
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].SubDivision,
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].City,
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].PostalCode,
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].MainDivision,
resCleansedMulti.TaxAreaResult[0].confidenceIndicator
};
File.AppendAllLines(pathCleansed, createText, System.Text.Encoding.UTF8);
These are the current results: current results
This is what I would like it to do: desired results
I have had good success with CsvHelper package. You can find more information about it here https://joshclose.github.io/CsvHelper/api/CsvHelper/CsvWriter/.
This helper implements IDisposable so be sure to dispose if it when you're done or wrap it in a using which is more preferred. You will have to provide a writer object to CsvHelper. In the past I've used MemoryStream and StreamWriter.
//Headers if you want
csvWriter.WriteField("StreetAddress1");
csvWriter.WriteField("StreetAddress2");
csvWriter.WriteField("subDivision");
csvWriter.WriteField("City");
csvWriter.WriteField("PostalCode");
csvWriter.WriteField("MainDivision");
csvWriter.WriteField("ConfidenceIndicator");
csvWriter.NextRecord();
//Your Loop Here
{
csvWriter.WriteField(resCleansedMulti.TaxAreaResult[0].PostalAddress[0].StreetAddress1);
csvWriter.WriteField(resCleansedMulti.TaxAreaResult[0].PostalAddress[0].StreetAddress2);
csvWriter.WriteField(resCleansedMulti.TaxAreaResult[0].PostalAddress[0].SubDivision);
csvWriter.WriteField(resCleansedMulti.TaxAreaResult[0].PostalAddress[0].City);
csvWriter.WriteField(resCleansedMulti.TaxAreaResult[0].PostalAddress[0].PostalCode);
csvWriter.WriteField(resCleansedMulti.TaxAreaResult[0].PostalAddress[0].MainDivision);
csvWriter.WriteField(resCleansedMulti.TaxAreaResult[0].confidenceIndicator);
csvWriter.NextRecord();
}
Update: I was able to get the desired results by changing to code to:
string pathCleansed = #"myfilename.csv";
string[] createText = {
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].StreetAddress1 + "," +
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].StreetAddress2 + "," +
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].SubDivision + "," +
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].City + "," +
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].PostalCode + "," +
resCleansedMulti.TaxAreaResult[0].PostalAddress[0].MainDivision + "," +
resCleansedMulti.TaxAreaResult[0].confidenceIndicator
};
File.AppendAllLines(pathCleansed, createText, System.Text.Encoding.UTF8);
I'm currently pooping out, I am having trouble displaying lines from a text document. What I mean is whenever I press enter it displays a new line of the text document.
Thanks
This is I guess some code, this is referenced to a text document and received a definition, from here it receives it and gets it ready to display but I just need it to read from say result one line at a time every time I press enter
First line wont work this is the first line ->>>>
var result = GetLinesWithWord(i1, #"" + Path + "/dict2.txt");
// Display the results.
foreach (var line1 in result)
{
//word maxlength
const int MaxLength = 82;
var name1 = line1;
if (name1.Length > MaxLength)
name1 = name1.Substring(0, MaxLength);
Console.WriteLine(name1 + "\r");
string boc1 = name1;
string foc1 = i1;
System.IO.File.AppendAllText (#"" + Path + "/" + n + ".txt", foc1 + "\n" + boc1 + "\n");
If I understand you correctly (especcialy your comment to #MairajAhmad) you want to dump the text file line by line, waiting for the user to press a key after each line.
Look at the post here. I think, that is what you need. It's basically a call to Console.ReadKey(true). The mentioned post listen's for the escape key, but it should still illustrate what to do.
I'm creating a standard calculator with a history feature. Previous solutions will show in a label box every time the user clicks the "=" button
After converting this string:
string histo = (operand1 + " " + operation + " " + " " + operand2 + " = " + result);
To a list by using this code:
List<string> hist = histo.Split().ToList()
What I want to do next is to print it to a label box to show history. How can I do that? Thank you.
Your question is not very clear and I do not think that label is a good choice to show the history. Combobox would be a better option, if you want to allow users to select items from history.
As far as your question on how to display it in a label is concerned you can use the following code. You can replace "," with Environment.NewLine.
lbl.Text = String.Join(",", hist);
foreach (string TempSize in sizeArr)
{
txtResult.Text += store + "\t" + attributeSet + "\t" + configurableAttri + "\t" + type + "\t" + (ID + "_" + TempColour + TempSize) + "\n";
}
I am trying to generate Multiple Rows that i can use to paste into excel file. "\n" doesn't seem to show on the "new line" of the textbox.
I tried manually doing "\n" aka shift+enter and copy paste the 3 rows into excel, Excel place a empty space between each of them, anyway to remove that problem too?
Thanks in advance!
Problem Fixed with "\r\n" instead of "\n" hat's how Windows controls represent newlines
I am currently developing an application in C# where I need to write a tab separated CSV file from the data that it retrieves from a MySQL Database. The database retrieval works fine.
The problem that I am having is writing the file. Between each variable that I am writing I am using the \t which I thought put a tab into the csv, therefore when opening in excel each variable will be in its own cell.
However for some reason it is not doing this it just writes the whole line as one long string. Below is an example of the code that I am code that I have written:
while (reader.Read())
{
int bankID = reader.GetInt16("ban_bankID");
int userID = reader.GetInt16("ban_userID");
string bankUsername = reader.GetString("ban_username");
string accountName = reader.GetString("ban_accountName");
string accountType = reader.GetString("ban_accountType");
decimal overdraft = reader.GetDecimal("ban_overdraft");
char defaultAccount = reader.GetChar("ban_defaultAccount");
string line = bankID + "\t" + userID + "\t" + bankUsername + "\t" + accountName + "\t"
+ accountType + "\t" + overdraft + "\t" + defaultAccount + "\n";
tw.WriteLine(line);
Thanks for your help with this problem.
The format is correct, a CSV expects the file to be COMMA Separated. When saving a Tab delimited file, typically just a txt extension is used (or some people save as .tsv) etc.
If you look at the Save As options in excel the option is Text (Tab Delimited) .txt
If I open the output generated by your sample code (stubbing in the data) everything loads in to Excel 2007 as you would expect.
The problem is your encoding.
You don't show your TextWriter instantiation, but it should look something like this:
TextWriter tw = new Stream(filename, false, Encoding.ASCII);
You should use the Text Import Wizard: Data / From Text. From there you can specify your delimiter to a tab.