In my page, I have a dropdown list contains filefolders and after I select one it will check all text files under the folder and remove the blank lines in the end.
First time, It was okay and removed all blank lines, and then When I select the dropdown list again, it throws an exception. Maybe it does not blank line which caused the exception, not sure.
The code is here:
public static void ClearBlank(string file)
{
string[] lines;
try
{
if (File.Exists(file))
{
lines = File.ReadAllLines(file);
File.Delete(file);
using (FileStream fs = new FileStream(file, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs))
//using (StreamWriter sw = new StreamWriter(file))
{
//foreach (string line in lines)
for (int i = 0; i < lines.Length - 1; i++)
{
if (!String.IsNullOrWhiteSpace(lines[i]))
// if (!String.IsNullOrWhiteSpace(line))
//.IsNullOrEmpty(line) ...
Go to the complete details ...