I need some help with file streaming. I have a program that will record names into a string array that will be written to a .txt file. Then if a user wants to delete a specific string from the array in the .txt file. the program will search for the string in the array in the .txt file, copy all the lines in the .txt file except the matching string. Then, paste all of the lines in a temp .txt file except the 1 matching string. Then copy and paste all of the string lines in the temp .txt file back to the original .txt file and then delete the temp file. The issue that i am having is, i can't figure out how to copy all of the string lines to the temp .txt file. I know that they are not getting copied because i am use a listbox (for diagnostic reasons) to output everything in the temp file, but nothing ever get displayed. I even F8 through the whole process and it seems like its writing, but it really isnt. I cant even start thinking about the rest of the programing until this part is solved and i need to know why it isnt writing to the temp file. Any help you provide would be really help. Thank you again.
string[] names = File.ReadAllLines("name.txt");
string fullName, firstName, lastName;
fullName = tbInput.Text.ToUpper();
double Fullname;
int space;
int fullNameLength;
if (fullName.Contains(" "))
{
//This is for the Last name//
space = fullName.IndexOf(" ");
fullNameLength = fullName.Length;
space++;
lastName = fullName.Substring(space, fullNameLength - space);
//This is for the first name//
firstName = fullName.Substring(0, space);
fullName = lastName + "," + firstName;
//If the input name is valid, then it will procceed to the next if statment//
Array.Sort(names);
//if the fullname matches a string in the array, get the position. If no match is found then name was never recorded//
int Position = Array.IndexOf(names, fullName);
//Since arrays start at 0, if a position is found, the position WILL be greater than -1, so run the if statment//.
if (Position > -1)
{
//Please ingnore these 2 lines, these are a work in progress for when i can move onto the next step of deletion and what not.//
// FileStream name = new FileStream("name.txt", FileMode.Open, FileAccess.Write);
//StreamWriter write = new StreamWriter(name);
//I want to Open the Temps.txt file and seek the last line in the file to indicate where it need to write the next string//
//while looping through the array "names"//
for (int i = 0; i < names.Length; i++)
{
//While looping through the array, "i" will increase by 1 each time the loop runs, when "i" equals the position(or index)//
//skip it. For everything else, read the line of the current index in the arry and write it to the temp.txt file//
if (i == Position)
{
names.Skip(Position);
}
else
{
FileStream sw = new FileStream("Temp.txt", FileMode.Append, FileAccess.Write);
StreamWriter write2 = new StreamWriter(sw);
string input = names[i];
write2.WriteLine(input);
sw.Close();
}
}
//This part is used to loop through the temp file and output to a temp listbox to see if data is actually writing//
string[] Temp = File.ReadAllLines("Temp.txt");
for (int j = 0; j < Temp.Length; j++)
{
lbtest.Items.Add(Temp[j]);
}
I added many comments so everyone can get something of an understanding of my thought process.
Why bother with the temp file? Since you're doing a File.ReadAllLines()
store that result in a List<string>
. Perform an IndexOf()
check is in the List and remove it if it's greater than -1 (You're doing that with your Position variable). Then use File.WriteAllLines()
and give it your list.
List<string> names = new List<string>(File.ReadAllLines("name.txt"));
string fullName = tbInput.Text.ToUpper();
int fullNameIndex = names.IndexOf(fullName);
if (fullNameIndex > -1)
{
names.RemoveAt(fullNameIndex);
}
File.WriteAllLines("name.txt", names);