.Net 中的临时文件

发布于 2024-08-05 04:09:06 字数 126 浏览 3 评论 0原文

我需要创建一些位图并将它们保存到文件系统中。由于某种原因,MScharting 系统希望其后台文件以字符串形式的路径提供。

我正在动态创建背景图像,尽管只有几次。

创建这些文件然后清理它们的最佳方法是什么?

I need to create some bitmaps and save them to the file system. For some reason the MScharting system wants it's background files supplies as paths in string form.

I'm dynamically creating the background image, although only a few times.

What's the best way to create these files and then clean them up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

分開簡單 2024-08-12 04:09:06

以下是获取临时文件的完整路径和文件名的方法:

string tempFile = System.IO.Path.GetTempFileName();

使用此文件名创建文件并创建临时文件。路径,完成后将其删除。

Here's how you can get the full path and file name of a temporary file:

string tempFile = System.IO.Path.GetTempFileName();

Create the file using this filename & path and when you're done delete it.

电影里的梦 2024-08-12 04:09:06

最好的选择是拥有一个实现 IDisposable 的 TemporaryFileManager;你要求它提供临时文件,它会自动生成并粘贴在某个临时目录中,然后当 TemporaryFileManager 被你或终结器释放时,它们都会被删除(如果你已经正确实现了一次性模式)

Your best bet is to have a TemporaryFileManager that implements IDisposable; you ask it for temporary files, which it auto-generates and sticks in a temp directory somewhere, then they all get deleted when the TemporaryFileManager gets disposed, either by you or the finalizer (if you've implemented the disposable pattern correctly)

信愁 2024-08-12 04:09:06

在我的项目中,我有一个名为 TempFile 的辅助类。它有几个静态方法,我用它们将流(或字节数组,如果需要)写入临时文件。这是此类方法的一个简化示例:

public static string Write(Stream stream)
{
   string FileName = Path.GetTempFileName();

   // Write the contents of stream to a file with FileName

   return FileName;
}

然后,我有另一个方法接受文件路径以供稍后删除,该方法是我的“解析”类的成员,尽管您可以将其放入其自己的静态帮助器类中:

public string ForDeletion(string path)
{
   ListOfPaths.Add(path);

   return path;
}

最后,我这样做以下

SomeApiFunction(ForDeletion(TempFile.Write(myStream)));

是我想出的解决 API 缺乏流处理功能的最佳方法。

In my projects, I have a helper class called TempFile. It has several static methods that I use to write a stream (or an array of bytes if need be) to a temporary file. Here's a simplified example of such a method:

public static string Write(Stream stream)
{
   string FileName = Path.GetTempFileName();

   // Write the contents of stream to a file with FileName

   return FileName;
}

Then, I have another method that accepts a file path for later deletion which is a member of my 'parsing' class, although you could put it in its own static helper class:

public string ForDeletion(string path)
{
   ListOfPaths.Add(path);

   return path;
}

Finally, I do the following:

SomeApiFunction(ForDeletion(TempFile.Write(myStream)));

This is the best way I've come up with for circumventing an API's lack of stream handling capabilities.

习惯成性 2024-08-12 04:09:06

我使用这个解决方案:

using System.IO;
using System.Reflection;

namespace Konard.Helpers
{
    public static partial class TemporaryFiles
    {
        private const string UserFilesListFilenamePrefix = ".used-temporary-files.txt";
        static private readonly object UsedFilesListLock = new object();

        private static string GetUsedFilesListFilename()
        {
            return Assembly.GetEntryAssembly().Location + UserFilesListFilenamePrefix;
        }

        private static void AddToUsedFilesList(string filename)
        {
            lock (UsedFilesListLock)
            {
                using (var writer = File.AppendText(GetUsedFilesListFilename()))
                    writer.WriteLine(filename);
            }
        }

        public static string UseNew()
        {
            var filename = Path.GetTempFileName();
            AddToUsedFilesList(filename);
            return filename;
        }

        public static void DeleteAllPreviouslyUsed()
        {
            lock (UsedFilesListLock)
            {
                var usedFilesListFilename = GetUsedFilesListFilename();

                if (!File.Exists(usedFilesListFilename))
                    return;

                using (var listFile = File.Open(usedFilesListFilename, FileMode.Open))
                {
                    using (var reader = new StreamReader(listFile))
                    {
                        string tempFileToDelete;
                        while ((tempFileToDelete = reader.ReadLine()) != null)
                        {
                            if (File.Exists(tempFileToDelete))
                                File.Delete(tempFileToDelete);
                        }
                    }
                }

                // Clean up
                using (File.Open(usedFilesListFilename, FileMode.Truncate)) { }
            }
        }
    }
}

每次需要临时文件时,请使用:

var tempFile = TemporaryFiles.UseNew();

为了确保在应用程序关闭或崩溃后将所有临时文件删除,放在

TemporaryFiles.DeleteAllPreviouslyUsed();

应用程序启动时。

I use this solution:

using System.IO;
using System.Reflection;

namespace Konard.Helpers
{
    public static partial class TemporaryFiles
    {
        private const string UserFilesListFilenamePrefix = ".used-temporary-files.txt";
        static private readonly object UsedFilesListLock = new object();

        private static string GetUsedFilesListFilename()
        {
            return Assembly.GetEntryAssembly().Location + UserFilesListFilenamePrefix;
        }

        private static void AddToUsedFilesList(string filename)
        {
            lock (UsedFilesListLock)
            {
                using (var writer = File.AppendText(GetUsedFilesListFilename()))
                    writer.WriteLine(filename);
            }
        }

        public static string UseNew()
        {
            var filename = Path.GetTempFileName();
            AddToUsedFilesList(filename);
            return filename;
        }

        public static void DeleteAllPreviouslyUsed()
        {
            lock (UsedFilesListLock)
            {
                var usedFilesListFilename = GetUsedFilesListFilename();

                if (!File.Exists(usedFilesListFilename))
                    return;

                using (var listFile = File.Open(usedFilesListFilename, FileMode.Open))
                {
                    using (var reader = new StreamReader(listFile))
                    {
                        string tempFileToDelete;
                        while ((tempFileToDelete = reader.ReadLine()) != null)
                        {
                            if (File.Exists(tempFileToDelete))
                                File.Delete(tempFileToDelete);
                        }
                    }
                }

                // Clean up
                using (File.Open(usedFilesListFilename, FileMode.Truncate)) { }
            }
        }
    }
}

Every time you need temporary file use:

var tempFile = TemporaryFiles.UseNew();

To be sure all temporary files are deleted after application closes or crashes put

TemporaryFiles.DeleteAllPreviouslyUsed();

at start of the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文