保存 .txt 文件的文件路径
我需要我的应用程序要求用户浏览到特定文件,保存该文件位置,然后将文本框中的字符串写入其中。
但是,我只需要最终用户在应用程序第一次启动时浏览到该文件。 只有一次。
这就是我的困境,如果我的应用程序是第一次启动,我怎样才能记住?
I need my application to ask the user to browse to a particular file, save that files location and subsequently write a string from a TextBox to it.
However, I only need my end-user to browse to the file the first time the application launches. Only once.
Here lies my dilemma, how can I have my application remember if it was the first time it launched?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你想要一个文件夹,而不是文件,但这不是重点。
您可以使用 UserSetting(请参阅项目属性、设置)并使用空值或无效值进行部署。 仅当您从设置中读取无效值时,才会启动对话框。
这是基于每个用户的。
您可以在 .NET 中使用注册表,但您确实希望尽可能远离它。 该库不在系统名称空间中这一事实是一个指标。
I think you want a folder, not a file, but that is besides the point.
You can use a UserSetting (See Project properties, Settings) and deploy it with an empty or invalid value. Only when you read the invalid value from settings do you start the Dialog.
This is on a per-user basis.
You can use the Registry in .NET but you really want to stay away from that as much as possible. The fact that the library is not in a System namespace is an indicator.
将所选文件保存在注册表中,或保存在用户的 Documents and Settings 文件夹中的配置文件中。
要访问本地程序的路径,请使用:
Save the file chosen in the registry, or in a configuration file in the user's Documents and Settings folder.
To get to your local program's path, use:
我将使用注册表为您的应用程序添加“SavedFileLocation”条目。
有关使用注册表的教程,请查看此处。
然后您可以检查密钥是否存在,如果不存在则显示对话框。
如果密钥存在,您应该检查文件是否存在。 如果该文件不存在,您可能应该向用户提供此信息,并询问他们是否要在此处创建新文件,或选择新位置。
否则,获取该值并将其保留以供运行时使用。
代码:
I would use the Registry to add an entry for "SavedFileLocation" for your application.
For a tutorial on using the registry, check here.
Then you can check if the key exists, if not present the dialog.
If the key exists, you should check for existence of the file. If the file does not exist, you should probably present this information to the user, and ask them if they want to create a new file there, or choose a new location.
Otherwise, take that value and keep it for runtime.
CODE: