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 8928645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:29:27+00:00 2026-06-15T08:29:27+00:00

I am writing a blog where I upload text documents to a directory that

  • 0

I am writing a blog where I upload text documents to a directory that contain HTML. Using the code below, do you anticipate I will have any issues with file locking or other problems I am not seeing? I am most concerned about the File.ReadAllText().

The directory will contain a list of files ex:

20120101_2300.txt

20120201_0100.txt

etc…

public class Website
{
    private string directory = "C:\\Web";
    public List<BlogEntry> GetArchives()
    {
        return GetArchives("");
    }

    public List<BlogEntry> GetArchives(string date)
    {
        var files = !string.IsNullOrEmpty(date) ? Directory.GetFiles(directory, "*.txt").Where(t => t.Contains(date)) : Directory.GetFiles("C:\\Web", "*.txt");
        var sb = files.Select(file => new BlogEntry {FullPath = file}).ToList();
        return sb.OrderByDescending(t => t.FileDate).Skip(5).ToList();
    }

    public List<BlogEntry> GetRecent()
    {
        var files = Directory.GetFiles(directory, "*.txt");
        var sb = files.Select(file => new BlogEntry {FullPath = file}).ToList();
        return sb.OrderByDescending(t => t.FileDate).Take(5).ToList();
    }
}

public class BlogEntry
{
    public string FullPath { get; set; }

    public DateTime FileDate
    {
        get { return DateTime.ParseExact(Path.GetFileNameWithoutExtension(FullPath), "yyyyMMdd_HHmm", CultureInfo.InvariantCulture); }
    }

    public string FileContents
    {
        get { return File.ReadAllText(FullPath); }
    }
}
  • 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. Editorial Team
    Editorial Team
    2026-06-15T08:29:29+00:00Added an answer on June 15, 2026 at 8:29 am

    As long as you’re sure there’s only one writer to a file (ie: yourself), this can work. More than that, and you’re likely to get into trouble. If you’re okay with this restriction, though, all you need to do is make sure to use a FileStream with the appropriate FileShare enumeration. It’s pretty easy if you wrap it in a StreamReader.

    Write the property like this:

    public string FileContents
    {
        get { 
           using (var fs = new FileStream(FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
           using (var rdr = new StreamReader(fs))
           {
              return rdr.ReadToEnd();
           }
    
        }
    }
    

    For performance, if your server has the ram, you’ll like do much better if you load these into a dictionary in memory the first time each record is asked for. Your code would look in the dictionary first and only read the file if the dictionary doesn’t have it.

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

Sidebar

Related Questions

I have started writing articles for my blog recently and then realized that google
I am writing my first Blog using Html&Css, Php And MySql. While writing the
I want to start writing some blog and I know that i will past
I am writing a code to post html-based blog posts through jquery/ajax to php
I am using this article of architecture http://blog.extjs.eu/know-how/writing-a-big-application-in-ext/ I have this one function where
I'm writing a simple blog using Django, and I'm including a Python CLI script
I am writing my own blog (every one should, it's good experience). I have
I have been writing articles on the wordpress.com blog , now I am looking
I'm writing a blog on Java, now I have have 2 servlets models, in
I'm writing a blog editor using pyqt4. My issue is this. There is a

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.