程序启动时自动加载文件

发布于 2024-12-03 18:30:32 字数 582 浏览 1 评论 0原文

好的,这是一些代码。我遇到一个问题,我想要保存到预定义的位置,并且我想要为文件指定一个预定义的名称。根据我在 MSDN 上看到的内容,据我所知,FileStream 和 StreamWriter 都不允许您设置这两个参数。

FileStream fs = new FileStream("PermaServerList", FileMode.Create, FileAccess.Write);
StreamWriter hiddensw = new StreamWriter(@"Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments", false);

那么,如果您看一下,我将如何将名为“PermaServerList”的文件保存到“我的文档”位置,无论他们使用的 Windows 版本如何?我不想在某个位置进行硬编码,我希望它始终是“我的文档”在其特定版本中的位置。

或者,这背后的想法是,每次程序启动时,我希望它自动加载上次保存的列表。有没有一种简单的方法可以做到这一点?现在,我的想法是,我将保存到他们选择的位置,然后在我的预定义位置制作第二个副本,并在程序启动时加载它。有想法吗?

Ok, so here's a bit of code. I'm having an issue where I want to save to a pre-defined location, and I want to have a pre-defined name for a file. Neither FileStream nor StreamWriter allows you to set both of those paramters as far as I can tell, based on what I've seen on MSDN.

FileStream fs = new FileStream("PermaServerList", FileMode.Create, FileAccess.Write);
StreamWriter hiddensw = new StreamWriter(@"Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments", false);

So, if you look at that, how would I get it to save a file called "PermaServerList" to the location "My Documents", regardless of the version of Windows they're using? I don't want to hard code in a location, I want it to always be whatever My Documents is in their particular version.

Alternatively, the idea behind this is that every time the program starts, I want it to load the list they last saved automatically. Is there a -simple- way to do this? Right now, the idea is that I'll just save to their chosen location, and then make a second copy in my pre-defined location and just load that on program startup. Ideas?

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

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

发布评论

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

评论(2

一曲琵琶半遮面シ 2024-12-10 18:30:32

是的,您只是尝试存储和读取用户数据,这可以使用 app.config 设置文件

Yes, you're simply trying to store and read user data, which can be easily dealt with using app.config settings file.

旧夏天 2024-12-10 18:30:32
string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PermaServerList.txt");
using (StreamWriter writer = new StreamWriter(fileName)) {
    writer.WriteLine("wooo");
}

例如,这就是写入文件的方式。 SpecialFolfer 枚举每次都会为您提供“我的文档”目录的位置,无论他们使用什么版本的 Windows,或者该文件夹是否映射到网络位置等

。不确定“程序启动时加载文件”是什么意思;我认为您的问题是您需要目录位置,除此之外,这只是将其作为流打开并使用它的问题。

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PermaServerList.txt");
using (StreamWriter writer = new StreamWriter(fileName)) {
    writer.WriteLine("wooo");
}

That's how you'd write to the file, for example. The SpecialFolfer enum will get you the location of the "My Documents" directory every time, regardless of what version of Windows they're using, or whether the folder is mapped to a network location, etc.

I'm not sure what you mean by "load the file when the program starts"; I assume your issue is that you need the directory location, beyond that it's just a question of opening it as a stream and working with it.

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