保存 .txt 文件的文件路径

发布于 2024-07-30 19:55:25 字数 168 浏览 3 评论 0原文

我需要我的应用程序要求用户浏览到特定文件,保存该文件位置,然后将文本框中的字符串写入其中。

但是,我只需要最终用户在应用程序第一次启动时浏览到该文件。 只有一次。

这就是我的困境,如果我的应用程序是第一次启动,我怎样才能记住

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 技术交流群。

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

发布评论

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

评论(3

鹤仙姿 2024-08-06 19:55:25

我认为你想要一个文件夹,而不是文件,但这不是重点。

您可以使用 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.

吃不饱 2024-08-06 19:55:25

将所选文件保存在注册表中,或保存在用户的 Documents and Settings 文件夹中的配置文件中。

要访问本地程序的路径,请使用:

string path = Environment.GetFolderPath(Environment.LocalApplicationData);

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:

string path = Environment.GetFolderPath(Environment.LocalApplicationData);
戏舞 2024-08-06 19:55:25

我将使用注册表为您的应用程序添加“SavedFileLocation”条目。

有关使用注册表的教程,请查看此处

然后您可以检查密钥是否存在,如果不存在则显示对话框。
如果密钥存在,您应该检查文件是否存在。 如果该文件不存在,您可能应该向用户提供此信息,并询问他们是否要在此处创建新文件,或选择新位置。
否则,获取该值并将其保留以供运行时使用。

代码:

AppInitialization()
{
    RegistryKey appKey = Registry.CurrentUser.OpenSubKey(
        @"Software\YourName\YourApp"
        ?? Registry.CurrentUser.CreateSubKey( @"Software\YourName\YourApp" );


    this.fileLocation = appKey.GetValue( "SavedFileLocation" )
        ?? GetLocationFromDialog()
        ?? "DefaultFileInCurrentDirectory.txt";
}

private static string GetLocationFromDialog()
{
    string value = null;

    RegistryKey appKey = Registry.CurrentUser.OpenSubKey(
        @"Software\YourName\YourApp"
        ?? Registry.CurrentUser.CreateSubKey( @"Software\YourName\YourApp" );

    using( OpenFileDialog ofd = new OpenFileDialog() )
    {
        if( ofd.ShowDialog() == DialogResult.OK )
        {
            value = ofd.File;
            appKey.SetValue( "SavedFileLocation", value );
        }
    }

    return value;
}

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:

AppInitialization()
{
    RegistryKey appKey = Registry.CurrentUser.OpenSubKey(
        @"Software\YourName\YourApp"
        ?? Registry.CurrentUser.CreateSubKey( @"Software\YourName\YourApp" );


    this.fileLocation = appKey.GetValue( "SavedFileLocation" )
        ?? GetLocationFromDialog()
        ?? "DefaultFileInCurrentDirectory.txt";
}

private static string GetLocationFromDialog()
{
    string value = null;

    RegistryKey appKey = Registry.CurrentUser.OpenSubKey(
        @"Software\YourName\YourApp"
        ?? Registry.CurrentUser.CreateSubKey( @"Software\YourName\YourApp" );

    using( OpenFileDialog ofd = new OpenFileDialog() )
    {
        if( ofd.ShowDialog() == DialogResult.OK )
        {
            value = ofd.File;
            appKey.SetValue( "SavedFileLocation", value );
        }
    }

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