r3nder
asked on
Delete line from text file
I am trying to delete a line in a text
Here is the scenario:
there was an error like no connection to the database. so I have a streamwriter write the sql query to a text file - when there is a connection the code checks to see if the file has any lines - if it does it processes it . If the app successfully uploads that line of sql code I want to delete that line from the text file and continue to process the file till it is complete.
mycount is the count of lines in the text file
Here is the scenario:
there was an error like no connection to the database. so I have a streamwriter write the sql query to a text file - when there is a connection the code checks to see if the file has any lines - if it does it processes it . If the app successfully uploads that line of sql code I want to delete that line from the text file and continue to process the file till it is complete.
mycount is the count of lines in the text file
if (mycount > 0)
{
string line = string.Empty;
string newline = string.Empty;
int lineNumber = 0;
string sqlStr = string.Empty;
string sqlfile = "\\sqlfile.txt";
// string sqlfile1 = "\\sqlfilenew.txt";
bool iWrote = false;
DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory());
System.IO.StreamReader file = new System.IO.StreamReader(di.ToString() + sqlfile);
while ((sqlStr = file.ReadLine()) != null)
{
try
{
iWrote = ss.UploadBackLog(sqlStr);
file.Close();
if (iWrote)
{
System.IO.StreamReader cutline = new System.IO.StreamReader(di.ToString() + sqlfile);
System.IO.StreamWriter sw = new System.IO.StreamWriter(di.ToString() + sqlfile);
while ((line = cutline.ReadLine()) != null)
{
if ((lineNumber + 1).Equals(lineNumber))
{
//
}
else
{
// sw.WriteLine(line + Environment.NewLine);
}
lineNumber++;
}
file.Close();
}
}
catch(Exception ex)
{
iWrote = false;
}
}
ASKER
thanks Duy
I need to process them 1 at a time because the file has the potential to be large - I have done this but can only get it to proccess 1 line - how can I get it to process all lines
I need to process them 1 at a time because the file has the potential to be large - I have done this but can only get it to proccess 1 line - how can I get it to process all lines
private void processSQL(int mycount)
{
if (mycount > 0)
{
string line = string.Empty;
string newline = string.Empty;
string sqlStr = string.Empty;
string sqlfile = "\\sqlfile.txt";
string sqlfile1 = "\\sqlfilenew.txt";
bool iWrote = false;
DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory());
using (var newfile = File.Create(di.ToString() + sqlfile1))
{
//only used this to close the connection to the file after it was created
}
System.IO.StreamReader file = new System.IO.StreamReader(di.ToString() + sqlfile);
while ((sqlStr = file.ReadLine()) != null)
{
try
{
iWrote = ss.UploadBackLog(sqlStr);
file.Close();
if (iWrote)
{
System.IO.StreamReader cutline = new System.IO.StreamReader(di.ToString() + sqlfile);
System.IO.StreamWriter sw = new System.IO.StreamWriter(di.ToString() + sqlfile1);
while ((line = cutline.ReadLine()) != null)
{
if (line != sqlStr)
{
sw.WriteLine(line);
}
}
cutline.Close();
File.Delete(di.ToString() + sqlfile);
sw.Flush();
sw.Close();
File.Move(di.ToString() + sqlfile1, "sqlfile.txt");
}
}
catch (Exception ex)
{
iWrote = false;
}
}
file.Close();
}
}
ASKER
its like it goes through the while loop only once
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
awesome Duy Thank you for the help
However, if your text file is not that big, I suggest you do read all the lines from text file into memory, process them and save back the lines those haven't been processed. Sample code could be:
Open in new window