【问题标题】:Returning paths with explorer on WPF application在 WPF 应用程序上使用资源管理器返回路径
【发布时间】:2017-08-02 15:22:38
【问题描述】:

在我的 WPF 应用程序中,我需要保存和加载文件。为了做到这一点,现在我使用文件名并使用它在程序文件夹中创建一个文件:

SaveFile = Interaction.InputBox("Input the name of the file", "Save", "Stuff") + ".txt";
StreamWriter saver = new StreamWriter(SaveFile);
//then writes stuff in the file

我想通过使用 explorer.exe 来选择要保存的位置以及加载文件的相同位置来改进这一点,而不是使用 InputBox。我基本上需要一种通过资源管理器返回文件夹或文件路径的方法。我浏览了很多关于 SO 的问题,但我只找到了通过表单执行此操作的方法,上次我尝试在 WPF 应用程序中使用表单时,我基本上搞砸了 Visual Studio。
那么,您对如何做到这一点有任何提示吗?

【问题讨论】:

  • 如果您不想了解我所理解的 GUI 元素,您能否阐明您的应用程序的用户如何参与其中以及他应该如何选择文件及其位置?
  • 请注意,标准用户没有“在程序文件夹中创建文件”的权限,如果用户修复或卸载您的程序,您在那里创建的任何内容都可能被删除。您可能应该在用户的ApplicationData 文件夹中创建文件。
  • @Linkø 直截了当地说:您想将文件系统的视图合并到您的程序中,以便他可以选择一个位置而不是使用对话框?

标签: c# wpf visual-studio wpf-controls


【解决方案1】:

假设您只需要一个简单的文件选择器,您可以采用以下方法。否则我不太了解您的要求,不需要表单,但仍然让用户可以选择使用某种形式的 GUI 控件来选择文件夹。

从 WPF 教程中尝试使用它 => The OpenFileDialog:

# xml required to set up the example as far as I can tell
<Window x:Class="WpfTutorialSamples.Dialogs.OpenFileDialogSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="OpenFileDialogSample" Height="300" Width="300">
    <DockPanel Margin="10">
        <WrapPanel HorizontalAlignment="Center" DockPanel.Dock="Top" Margin="0,0,0,10">
            <Button Name="btnOpenFile" Click="btnOpenFile_Click">Open file</Button>
        </WrapPanel>
        <TextBox Name="txtEditor" />
    </DockPanel>
</Window>

实际的 C# 代码

using System;
using System.IO;
using System.Windows;
using Microsoft.Win32;

namespace WpfTutorialSamples.Dialogs
{
        public partial class OpenFileDialogSample : Window
        {
                public OpenFileDialogSample()
                {
                        InitializeComponent();
                }

                private void btnOpenFile_Click(object sender, RoutedEventArgs e)
                {
                        OpenFileDialog openFileDialog = new OpenFileDialog();
                        if(openFileDialog.ShowDialog() == true)
                                txtEditor.Text = File.ReadAllText(openFileDialog.FileName);
                }
        }
}

【讨论】:

  • @Linkø 添加了另一个答案,显示了使用来自同一 WPF 教程站点的 SaveFileDialog 的不同但相似的示例。此外,如果您想推出自己的解决方案,如何遍历文件系统,收集所有现有目录到一定深度,并以某种树视图 gui 控件将它们呈现给用户。如果您只加载第一级节点,然后在用户请求时加载每个节点的内容,那么遍历文件系统部分可能需要非常高效。
【解决方案2】:

那么这里是 another example 来自同一个站点的副本并粘贴只是为了保存文件,其中您必须使用相同的 XML 来复制我假设的工作示例:

using System;
using System.IO;
using System.Windows;
using Microsoft.Win32;

namespace WpfTutorialSamples.Dialogs
{
        public partial class SaveFileDialogSample : Window
        {
                public SaveFileDialogSample()
                {
                        InitializeComponent();
                }

                private void btnSaveFile_Click(object sender, RoutedEventArgs e)
                {
                        SaveFileDialog saveFileDialog = new SaveFileDialog();
                        if(saveFileDialog.ShowDialog() == true)
                                File.WriteAllText(saveFileDialog.FileName, txtEditor.Text);
                }
        }
}

对话框如下所示

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多