开发者

C# and Storing Data in Memory

I'm using Visual C#.NET, and am开发者_如何学运维 making an app that uses winforms. I essentially need to open multiple files as strings, and manipulate the data in various places without saving the information back to the file. How do I go about storing this data so that I may use it in various parts of my code?


Create a singleton which contains the file strings as public properties, and make the singleton public.

http://msdn.microsoft.com/en-us/library/ms998558.aspx


Why open them "as strings"? Strings in .NET are immutable, so it could get expensive if you are making lots of changes. The normal approach would be to parse / deserialize the data out into an object model, and pass that object model into your forms - i.e.

MyModel model = MyModel.Load(path);
MyForm form = new MyForm();
form.Model = model;

or similar. Then your form can access properties of the model:

captionTextBox.Text = model.Title; // etc

or use data-binding if you really want:

captionTextBox.DataBindings.Add("Text", model, "Title");

(which will enable 1-way or 2-way binding, depending on whether your model also provides change-notifications)


I'd probably use a static class, unless there are other requirements you didn't mention.

public static class MyFilesAsStrings
{
    public static String FirstFile {get;set;}

    public static LoadData() 
    {
        FirstFile = System.IO.File.ReadAllText(@"C:\Temp\MyFile.dat");
        // and so on
    }
}


There are a number of approaches you could take.

If the files are small to medium sized, you could load the contents into a StringBuilder or a StringWriter and store these in a Dictionary indexed by file name.

If the files are large, you could do this, but you might need to consider saving the contents of the StringWriter to a temporary file while working with them, and reload from the temporary file as needed.

If the files are huge, you would read only parts, or pages, from the file, and manage those in the same way as the small files, except there'd be many per individual file on disk, and so you might store them in a List or similar.

There's also always the "copy-to-temp-file-edit-then-copy-back" approach. Simple, easy and effective, even if it does use the disk a lot.


I'm not sure that I understand what a Singleton gives you in this situation, other than ensuring you don't have multiple copies of the strings hanging around. It does nothing to help with modifying the strings themselves.

The way I read your question is that the manipulation of the strings in memory (without writing back to a file) is critical. Based on this there are two main options, StringBuilder and MemoryStream.

StringBuilder is specifically designed to allow you to efficiently modify or append to data represented as a string. If the file is not too large for it to fit in memory, StringBuilder is the best choice.

You simply pass the contents of the file to the StringBuilder constructor. Then utilize methods such as Append(), Insert(), Remove(), Replace() or the indexer [] to modify the string data as needed. The Stringbuilder ensures that these operations are much more efficient than doing the same on a standard string.

You could also load the file into a MemoryStream and then use a StringReader (or StringWriter) to get a Stream like interface (Read(), Peek(), ReadLine() etc) for manipulating the string.

It's a bit more work than StringBuilder, but may be preferred if a Stream style approach fits better with your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜