0

I use the File.WriteAllLines() method in my project. The txt file is to be synced to a remote server (via another application, Autodesk Docs in my case) upon Save action.

I am not sure why, but using WriteAllLines does not trigger syncing of the file, while opening it and performing a save manually does trigger the sync.

I suspect it's maybe due to WriteAllLines deleting the existing file and creating a new one instead of overwriting the data inside the file...? Is there a way to overwrite by deleting the existing the data in the file instead of deleting the existing file and creating a new file?

Am I even right about WriteAllLines deleting the existing file and recreating it?

4
  • I don't know if it'll work, but you could try using FileStream.SetLength to set the length of the file to zero and then write the data to that stream. Using fs = New FileStream("C:\temp\aab.txt", FileMode.OpenOrCreate) fs.SetLength(0) Using sw = New StreamWriter(fs) 'write text... etc. Commented Sep 28, 2023 at 10:35
  • 1
    You can investigate what happens to the file by using Process Monitor with suitable filters. Commented Sep 28, 2023 at 13:01
  • You can test your theory with File.WriteAllLines("temp.txt", Guid.NewGuid().ToString().Select(Function(c) c.ToString())) repeatedly and see that the creation date will remain the same while the modification date is changed. The file is not deleted
    – djv
    Commented Sep 28, 2023 at 14:44
  • You can also try different methods: Using sw As New StreamWriter(filename, False), Using fs As New FileStream(filename, FileMode.Truncate) Using sw As New StreamWriter(fs), Using sw As New StreamWriter(filename, True), Using fs As New FileStream(filename, FileMode.Create) Using sw As New StreamWriter(fs)
    – djv
    Commented Sep 28, 2023 at 15:28

0

Browse other questions tagged or ask your own question.