0

I read all line from file text into List<>.

But I have a problem. I want to change only 1 element(is columns) in 1 rows of file text.

When I send Email successful, it will update time in this line I checking.

This data in file text

    User1             abc@gmail.com           5/1/2016

// many rows.....

After SendEmail successful it's will update like:

User1             abc@gmail.com           8/1/2016

Current, I must use AppendAllLine() function to add line:

File.AppendAllLines(dataPath, (from i in Items select i.Serialize()).ToArray(), Encoding.Default);

I tried StreamWriter but it does not support like:

from i in Items select i.Serialize()).ToArray()

Here my code to update time.

if (time.Day == today.Day)
{
    flag = SendMail.sendDailyMail(listMail);
    if (flag == true)
    {
        rows.time = timeNow;
        List<DTOSaveFromFile> lst = new List<DTOSaveFromFile>(1);
        lst.Add(rows);
        DiskFile.Save(lst);
    }
}

Class DiskFile:

public static void Save(List<DTOSaveFromFile> Items)
{
    File.AppendAllLines(dataPath, (from i in Items select i.Serialize()).ToArray(), Encoding.Default);
}

public static List<DTOSaveFromFile> Load()
{
    string[] data = File.ReadAllLines(dataPath);
    return (from i in data select new DTOSaveFromFile(i)).ToList<DTOSaveFromFile>();
}
  • Dear friend, could you please set you question more focused. What excectly problem you have, what errors you have and so on. Best regards! – Yaugen Vlasau Jan 08 '16 at 08:31
  • I don't know how to change only `5/1/2016` to `8/1/2016` in this case. Only change this element. Don't add 1 line. Because this my code always add 1 line. I want to replace this time in this rows. – lkajfds Jan 08 '16 at 08:38
  • There does not seem to be any random access disk file features readily available. It seems your main choice is to read the whole file in; find and alter the line, and then write it back out. Must it be a disk file? Do you have access to an SQL server of any sort? – Peter Smith Jan 08 '16 at 08:41
  • I think all only change class DiskFile.Save() because File.AppenAllLine allows add to the new line. Not replace this line. – lkajfds Jan 08 '16 at 08:42
  • `lst` variable is collect all data in file .txt. I don't access to SQL server. I want SendMail success it will update time was send into file txt. – lkajfds Jan 08 '16 at 08:45

1 Answers1

0

The simple way is to delete the content of your file and then recreate a new file with all data updated:

public static void Save(List<DTOSaveFromFile> Items)
{
    File.Create(dataPath);
    File.AppendAllLines(dataPath, (from i in Items select i.Serialize()).ToArray(), Encoding.Default);
}

If your list contains all data you don't lost anything.

erikscandola
  • 2,854
  • 2
  • 17
  • 24
  • How to delete this file? And I'm in many foreach. This will happen occurs? – lkajfds Jan 08 '16 at 09:06
  • In your code I can't see any foreach. You don't need to delete the file because `Create` already delete the file if it exists. In my solution you have to call Save at the end of all updates to your items in `List`. – erikscandola Jan 08 '16 at 09:09