how do i delete lines from .txt file? "Urgent!"
-
How do i delete this lines after i pass the info to my listview?? using (StreamReader sr = new StreamReader(path1 + "/personalpr.txt")) { while (sr.Peek() > 0) { line = sr.ReadLine(); if (line == "#") { name = sr.ReadLine(); nameAndJob = string.Concat(name); Job = string.Concat(name); if (list.ContainsKey(nameAndJob)) { item1 = list[nameAndJob]; item1 = list[Job]; item1.SubItems[1].Text = sr.ReadLine(); item1.SubItems[3].Text = sr.ReadLine(); item1.SubItems[5].Text = sr.ReadLine(); /////////// Can i delete this lines in .txt file } } } my .txt file look like this: # 1 2 3 4 # 5 6 7 8 :confused:
-
How do i delete this lines after i pass the info to my listview?? using (StreamReader sr = new StreamReader(path1 + "/personalpr.txt")) { while (sr.Peek() > 0) { line = sr.ReadLine(); if (line == "#") { name = sr.ReadLine(); nameAndJob = string.Concat(name); Job = string.Concat(name); if (list.ContainsKey(nameAndJob)) { item1 = list[nameAndJob]; item1 = list[Job]; item1.SubItems[1].Text = sr.ReadLine(); item1.SubItems[3].Text = sr.ReadLine(); item1.SubItems[5].Text = sr.ReadLine(); /////////// Can i delete this lines in .txt file } } } my .txt file look like this: # 1 2 3 4 # 5 6 7 8 :confused:
Hi, you can do that if you create a temporary file, copy only the lines which should not be deleted to that temp file and later overwrite the original file with the temp file. E.g:
string tempFilename = System.IO.Path.GetTempFileName();
using (StreamReader sr = new StreamReader(path1 + ""))
using (StreamWriter sw = new StreamWriter(tempFilename))
{
while (sr.Peek() > 0)
{
line = sr.ReadLine();
if (line == "#")
{
name = sr.ReadLine();
nameAndJob = string.Concat(name);
Job = string.Concat(name);
if (list.ContainsKey(nameAndJob))
{
item1 = list[nameAndJob];
item1 = list[Job];
item1.SubItems[1].Text = sr.ReadLine();
item1.SubItems[3].Text = sr.ReadLine();
item1.SubItems[5].Text = sr.ReadLine();
/////////// Can i delete this lines in .txt file
}
else
{
// Write the "#" line
sw.WriteLine("#");
// Write the name which was read above
sw.WriteLine(name);
// The next three lines will be written in the else block below
}
}
else
{
sw.WriteLine(line);
}
}
sw.Flush();
}
System.IO.File.Delete(path1 + "/personalpr.txt");
System.IO.File.Move(tempFilename, path1 + "/personalpr.txt");I didn't test the code, but I think you want something like that.
-
How do i delete this lines after i pass the info to my listview?? using (StreamReader sr = new StreamReader(path1 + "/personalpr.txt")) { while (sr.Peek() > 0) { line = sr.ReadLine(); if (line == "#") { name = sr.ReadLine(); nameAndJob = string.Concat(name); Job = string.Concat(name); if (list.ContainsKey(nameAndJob)) { item1 = list[nameAndJob]; item1 = list[Job]; item1.SubItems[1].Text = sr.ReadLine(); item1.SubItems[3].Text = sr.ReadLine(); item1.SubItems[5].Text = sr.ReadLine(); /////////// Can i delete this lines in .txt file } } } my .txt file look like this: # 1 2 3 4 # 5 6 7 8 :confused:
-
Hi, you can do that if you create a temporary file, copy only the lines which should not be deleted to that temp file and later overwrite the original file with the temp file. E.g:
string tempFilename = System.IO.Path.GetTempFileName();
using (StreamReader sr = new StreamReader(path1 + ""))
using (StreamWriter sw = new StreamWriter(tempFilename))
{
while (sr.Peek() > 0)
{
line = sr.ReadLine();
if (line == "#")
{
name = sr.ReadLine();
nameAndJob = string.Concat(name);
Job = string.Concat(name);
if (list.ContainsKey(nameAndJob))
{
item1 = list[nameAndJob];
item1 = list[Job];
item1.SubItems[1].Text = sr.ReadLine();
item1.SubItems[3].Text = sr.ReadLine();
item1.SubItems[5].Text = sr.ReadLine();
/////////// Can i delete this lines in .txt file
}
else
{
// Write the "#" line
sw.WriteLine("#");
// Write the name which was read above
sw.WriteLine(name);
// The next three lines will be written in the else block below
}
}
else
{
sw.WriteLine(line);
}
}
sw.Flush();
}
System.IO.File.Delete(path1 + "/personalpr.txt");
System.IO.File.Move(tempFilename, path1 + "/personalpr.txt");I didn't test the code, but I think you want something like that.
it worked just fine, you are the best man!! now it only deletes the lines hwo gets in use, right? have another question for you because you are so good....:) can u help me with a counter for this: if subitem[0] and subitem[1] matches with a lines[idex +1] and a lines[idex +2] then calculate subitems[1] with +1. and the item are saved in .txt file like this # 1 2 3 4 # 5 6 7 8 :doh:
-
it worked just fine, you are the best man!! now it only deletes the lines hwo gets in use, right? have another question for you because you are so good....:) can u help me with a counter for this: if subitem[0] and subitem[1] matches with a lines[idex +1] and a lines[idex +2] then calculate subitems[1] with +1. and the item are saved in .txt file like this # 1 2 3 4 # 5 6 7 8 :doh:
andredani wrote:
now it only deletes the lines hwo gets in use, right?
Yes, it should only write the lines which are not used in the temp file (and therewith deleting the used lines).
andredani wrote:
can u help me with a counter for this: if subitem[0] and subitem[1] matches with a lines[idex +1] and a lines[idex +2] then calculate subitems[1] with +1.
I'm not quite sure that i understand what you mean. Let's assume your file looks like (I added line numbers): # (line 1) 5 (line 2) 6 (line 3) 5 (line 4) 6 (line 5) Normally your SubItems would be filled like this: SubItems[0] = 5 SubItems[1] = 6 SubItems[3] = 5 SubItems[5] = 6 But because line 2 = line 4 and line 3 = line 5 you want to add 1 to SubItems[1], so that the SubItems are: SubItems[0] = 5 SubItems[1] = 7 (6 from line 3 + 1) SubItems[3] = 5 SubItems[5] = 6 Is that what you mean?
-
andredani wrote:
now it only deletes the lines hwo gets in use, right?
Yes, it should only write the lines which are not used in the temp file (and therewith deleting the used lines).
andredani wrote:
can u help me with a counter for this: if subitem[0] and subitem[1] matches with a lines[idex +1] and a lines[idex +2] then calculate subitems[1] with +1.
I'm not quite sure that i understand what you mean. Let's assume your file looks like (I added line numbers): # (line 1) 5 (line 2) 6 (line 3) 5 (line 4) 6 (line 5) Normally your SubItems would be filled like this: SubItems[0] = 5 SubItems[1] = 6 SubItems[3] = 5 SubItems[5] = 6 But because line 2 = line 4 and line 3 = line 5 you want to add 1 to SubItems[1], so that the SubItems are: SubItems[0] = 5 SubItems[1] = 7 (6 from line 3 + 1) SubItems[3] = 5 SubItems[5] = 6 Is that what you mean?
ehh.. now i don´t understand you hehe...:)' i´m gonna try to excplain in easy terms... subitems[0] and subitem[1] have to be unique. .txt fil for example: # Online K411 2007-05-05 20:15:12 AD # Historik K411 2007-05-05 20:15:12 AD # Online K412 2007-05-05 20:15:12 AD Here is everything ok because line1 and line2 after# is unique. But this is wrong: # Online K412 2007-05-05 20:15:12 AD # Online K412 2007-05-05 20:15:12 AD In this example i have to add +1 on k412 to k413. so its unique. And also check k413 if its unique.. Little hard to excplain.. :-D Need this so bad...:^) Thanks man!
-
ehh.. now i don´t understand you hehe...:)' i´m gonna try to excplain in easy terms... subitems[0] and subitem[1] have to be unique. .txt fil for example: # Online K411 2007-05-05 20:15:12 AD # Historik K411 2007-05-05 20:15:12 AD # Online K412 2007-05-05 20:15:12 AD Here is everything ok because line1 and line2 after# is unique. But this is wrong: # Online K412 2007-05-05 20:15:12 AD # Online K412 2007-05-05 20:15:12 AD In this example i have to add +1 on k412 to k413. so its unique. And also check k413 if its unique.. Little hard to excplain.. :-D Need this so bad...:^) Thanks man!
Does the second line (K412 and K413 in your example) always start with one letter and is followed only by digits? Will there never be Km413 or just 413?
-
Does the second line (K412 and K413 in your example) always start with one letter and is followed only by digits? Will there never be Km413 or just 413?
yes always ONE letters and fallowed by 6 numbers... it makes all little bit harder... thanks!!! BUT the last number, is a string hwo starts with 1 like this: string ett = "1"; for exampel: K44322+(ett) = K443221 So "ett" is the only one hwo going to change! Thanks man i´m stuck now need your help!