【问题标题】:Possible to create a file in memory? [duplicate]可以在内存中创建文件吗? [复制]
【发布时间】:2014-08-19 16:31:33
【问题描述】:

我需要做的是在内存中创建一个文件,写入该文件,然后像访问任何其他文件一样访问该文件。

这里我正在创建和使用一个实际的文件:

if (!File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TEST.csv")))
{
    File.Create(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TEST.csv")).Dispose();
    using (TextWriter tw = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TEST.csv")))
    {
        foreach (string[] array in listOfRows)
        {
            string dataString = array[0] + "," + array[1] + "," + array[2] + "," + array[3];
            tw.WriteLine(dataString);
        }
        tw.Close();
    }
}
else if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TEST.csv")))
{
    using (TextWriter tw = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TEST.csv")))
    {
        foreach (string[] array in listOfRows)
        {
            string dataString = array[0] + "," + array[1] + "," + array[2] + "," + array[3];
            tw.WriteLine(dataString);
        }
        tw.Close();
    }
}

//makes virtual table to be copied to database
DataTable dt = new DataTable();
string line = null;
int i = 0;
using (StreamReader sr = File.OpenText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TEST.csv")))
{
    //etc etc etc
}

如您所见,目前我只是创建一个文件,然后将数据写入其中,然后使用该文件。如果我可以在虚拟内存中完成所有这些,对我来说会更好,这可能吗?

【问题讨论】:

  • 使用MemoryStream?
  • 为什么你觉得会好很多?除非存在特定的性能问题,否则您可能正在尝试解决不存在的问题。
  • 请花一些时间来修正你的代码缩进 - 它现在到处都是。
  • “在虚拟内存中执行所有这些操作” - 如“我想使用数组而不是强类型类的实例,在没有正确编码的情况下将其写入 CSV,然后将整个文件重新读取到 @ 987654323@ 而不是添加单行”?每个部分都有更好的方法......甚至比使用自定义内存代码击败文件需要更多的努力,大多数人都愿意投入。

标签: c#


【解决方案1】:

好吧,您可以使用MemoryStream 读取/写入字节数组。用这种方法你不会得到“线条”,但它很接近。

话虽如此,如果您立即写入和读取文件,这听起来就像是在兔子洞里。文件用于持久性

如果您只想在内存中存储一​​组字符串,您可以使用StringBuilder 或者只是List<T>

【讨论】:

  • +1 尽管你给另一个答案 +1 以表示“给他他想要的,而不是他可能需要的”,尽管当我得到答案或评论其中一个时,我经常感到恼火我的问题质疑我是否理智地要求我所要求的,我认为在这种情况下你指出他所要求的不太可能是他真正需要的,这是非常好的。
  • +1。请注意,MemoryStream 适用于少量数据 - 较大的数据集将通过MemoryStream 管理内存的方式(增长时复制而不是分块存储)遇到性能问题。
【解决方案2】:

听起来你想要一个内存映射文件(需要.Net 4 或更高版本)来自MSDN

using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;

class Program
{
    static void Main(string[] args)
    {
        long offset = 0x10000000; // 256 megabytes 
        long length = 0x20000000; // 512 megabytes 

        // Create the memory-mapped file. 
        using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\ExtremelyLargeImage.data", FileMode.Open,"ImgA"))
        {
            // Create a random access view, from the 256th megabyte (the offset) 
            // to the 768th megabyte (the offset plus length). 
            using (var accessor = mmf.CreateViewAccessor(offset, length))
            {
                int colorSize = Marshal.SizeOf(typeof(MyColor));
                MyColor color;

                // Make changes to the view. 
                for (long i = 0; i < length; i += colorSize)
                {
                    accessor.Read(i, out color);
                    color.Brighten(10);
                    accessor.Write(i, ref color);
                }
            }
        }
    }
}

public struct MyColor
{
    public short Red;
    public short Green;
    public short Blue;
    public short Alpha;

    // Make the view brighter. 
    public void Brighten(short value)
    {
        Red = (short)Math.Min(short.MaxValue, (int)Red + value);
        Green = (short)Math.Min(short.MaxValue, (int)Green + value);
        Blue = (short)Math.Min(short.MaxValue, (int)Blue + value);
        Alpha = (short)Math.Min(short.MaxValue, (int)Alpha + value);
    }
}

【讨论】:

  • +1 完全按照他的要求去做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-06
  • 2011-10-31
  • 2017-05-31
  • 2013-11-26
  • 1970-01-01
  • 2017-10-18
  • 2012-05-02
相关资源
最近更新 更多