Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 82579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:38:20+00:00 2026-05-10T21:38:20+00:00

We perform updates of large text files by writing new records to a temp

  • 0

We perform updates of large text files by writing new records to a temp file, then replacing the old file with the temp file. A heavily abbreviated version:

var tpath = Path.GetTempFileName(); try {     using (var sf = new StreamReader(sourcepath))     using (var tf = new StreamWriter(tpath))     {         string line;         while ((line = sf.ReadLine()) != null)             tf.WriteLine(UpdateLine(line));     }      File.Delete(sourcepath);     File.Move(tpath, sourcepath); } catch {     File.Delete(tpath);     throw; } 

If anything throws an exception (file not found, no permission), the original file is left untouched, which is what we want.

However, the code has the following problems:

  1. Is there a real-world situation where the Delete works but the Move fails? This would delete the original and updated data. This would be bad.

  2. The most common failure is the source file being open from another application, and the Delete fails. This means all the Update work is discarded. Is there a way to see if the source file is deletable at the start, and abandon the update if not?

  3. We have users putting Windows Explorer Summary properties, like Title or Comments, on files. These are discarded when we delete the file. Is there a way to copy the old file’s Summary properties to a new file? Should we do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:38:21+00:00Added an answer on May 10, 2026 at 9:38 pm

    Lots of good suggestions. I was able to solve the problems with:

    var sInfo = new FileInfo(sourcePath); if (sInfo.IsReadOnly)     throw new IOException('File '' + sInfo.FullName + '' is read-only.');  var tPath = Path.GetTempFileName(); try {     // This throws if sourcePath does not exist, is opened, or is not readable.     using (var sf = sInfo.OpenText())     using (var tf = new StreamWriter(tPath))     {         string line;         while ((line = sf.ReadLine()) != null)             tf.WriteLine(UpdateLine(line));     }      string backupPath = sInfo.FullName + '.bak';     if (File.Exists(backupPath))         File.Delete(backupPath);      File.Move(tPath, backupPath);     tPath = backupPath;     File.Replace(tPath, sInfo.FullName, null); } catch (Exception ex) {     File.Delete(tPath);     throw new IOException('File '' + sInfo.FullName + '' could not be overwritten.', ex); } 

    OpenText throws if the source file is open or not readable, and the update is not done. If anything throws, the original file is left unchanged. Replace copies the old files’ Summary properties to the new file. This works even if the source file is on a different volume than the temp folder.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer It definitely looks like a bug in the view merging… May 11, 2026 at 11:22 am
  • added an answer It is because of constant class loading. Java stores class… May 11, 2026 at 11:22 am
  • added an answer Sounds like you're probably CPU bound. All the programs you… May 11, 2026 at 11:22 am

Related Questions

We perform updates of large text files by writing new records to a temp
For desktop based applications, what are best practices to perform auto updates? Currently, we
When we perform a fork in Unix, open file handles are inherited, and if
We are developing a little application that given a directory with PDF files creates
We have been using CruiseControl for quite a while with NUnit and NAnt. For
We have a requirement in project to store all the revisions(Change History) for the
We have a remoting singleton server running in a separate windows service (let's call
We have an SVN repository running on a Windows server, and I want to
We currently maintain a suit of MFC applications that are fairly well designed, however
We have a Windows Server Web Edition 2003 Web Farm. What can we use

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.