Hi
Im totaly new to c#.
Q1: If i had a file like
Richard,Brennan
Jane,Smith
Paul,Grain
etc and a read each line 1 at a time, I assume that the whole line, 'Richard,Brennan' is read into the IO stream.
Whats the best way to get each name seperated into their own string?
(Right I have found split() and used it OK so ignore this one. Pls look at Q2.
Q2: How do i delete a specific line from a txt file?
And when i close the IO stream - does it save it and remove the white space?
Example, remove jane from above and close the gap.
Loading
Guest UserPosted Apr 6, 2011, 11:48 AM
VulpesPosted Apr 7, 2011, 10:49 AM
Richard BrennanPosted Apr 7, 2011, 9:04 AM
And i want to remove any user and white space.
Would the lizards idea work?
Sam HobbsPosted Apr 6, 2011, 8:58 PM
Also, it might work to simply search the data using Contains as theLizard shows but it might not work. It depends on what your requirements are.
theLizardPosted Apr 6, 2011, 8:31 PM
You don't need to copy or move anything...
List
string FileName = @"C:\myFile.Text";
StreamReader sr = new StreamReader(FileName);
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (!line.Contains("Jane"))
myLines.Add(line + Environment.NewLine);
// do whatever
}
sr.Close();
StreamWriter sw = new StreamWriter(FileName);
for (int i = 0; i < myLines.Count; i++)
sw.Write(myLines[i]);
sw.Flush();
sw.Close();
}
FroglegPosted Apr 6, 2011, 7:17 PM
File.Move("temp.txt","password.txt",true);
Sam HobbsPosted Apr 6, 2011, 6:58 PM
Also note that if you open and close a file then you need to do something such as delete or move the file, you might need to call Dispose() for the file object. If you get a sharing violation or if the system says it cannot access the file to do the move or delete, then probaly you need to Dispose() the file object first. Avoid calling Dispose() unless you know there is a need for it.
I am not sure, but I think that you don't need to do a Dispose() if you use a "using" statement for the file object.
Richard BrennanPosted Apr 6, 2011, 11:43 AM
but when i try to rename them using
File.Delete("pasword.txt");
File.Move("temp.txt","password.txt");
i get a run time error - can rename a file that exists - so the delete has not not worked - any ideas.
Holy do dar - spelling error lol - its sorted. What a silly billy. lol
Guest UserPosted Apr 6, 2011, 10:00 AM