在 WPF 应用程序中本地存储数据文件的文件夹 [英] Folder to store data files locally in WPF application

查看:45
本文介绍了在 WPF 应用程序中本地存储数据文件的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的 WPF 应用程序中有下面的代码,这正是我想要它做的,但是,发布后它不一定能够访问这些文件夹位置,因为它们不会指向正确的目录也不存在文件夹.

I currently have the code below in my WPF application which does exactly what I want it to do, however, upon publishing this it won't necessarily be able to access these folder locations as they won't be pointing to the correct directory nor will the folders exist.

我希望有人能够告诉我将某些内容保存到本地文件夹的最佳方法是什么?

I was hoping somebody might be able to tell me what is the best way to save something into a local folder?

它是否在应用程序文件夹本身内也没有问题.

Whether it's inside the application folder itself or not is of no issue either.

我目前用于写入文件的代码:

The code I'm currently using for the writing of the file:

using (Stream stream = File.Open(@"....Templatesdata.bin", FileMode.Create))
            {
                BinaryFormatter bin = new BinaryFormatter();
                bin.Serialize(stream, templateList);
            }

我目前用于加载文件的代码:

The code I'm currently using for the loading of the file:

using (Stream stream = File.Open(@"....Templatesdata.bin", FileMode.Open))
        {
            BinaryFormatter bin = new BinaryFormatter();

            templateList = (List<Template>)bin.Deserialize(stream);
        }

推荐答案

您可以使用 Environment.SpecialFolder 找到合适的位置来放置文件(例如,ApplicationData 将是一个不错的起点).如果您只需要一个临时文件,您可以使用 Path.GetTempFileName 来创建一个.

You can use Environment.SpecialFolder to find an appropriate place to put files (for example, ApplicationData would be a good place to start). If you only need a temp file, you can use Path.GetTempFileName to create one.

最后一个注释.在应用程序文件夹中存储东西本身可能是一个巨大的痛苦.通常,应用程序文件夹是在安装过程中使用管理员帐户创建的,因此您的应用程序在使用用户帐户运行时将无法写入该文件夹.

One last note. Storing stuff in the application folder itself can be a giant pain. Usually the application folder is created with the admin account during the installation so your app won't be able to write to it while running on a user account.

这篇关于在 WPF 应用程序中本地存储数据文件的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆