【问题标题】:How to get file path from folder wpf?如何从文件夹wpf中获取文件路径?
【发布时间】:2014-02-19 13:43:46
【问题描述】:

我遇到了一个问题,我正在开发一个 WPF 应用程序,其中我有一个名为“帮助”的页面,有一个按钮 go 当用户单击此按钮时,我必须向用户提供位于帮助文件夹中的 pdf 文件。 现在我的问题是如果我这样写路径

**string pdfurl1 = ((@"D:\addnkit\projects\wdine\widdne_working\Wdine Us\Wddine\Wine\Help\Emerald Wine Dispensing Software.pdf"));
                    System.Diagnostics.Process.Start(pdfurl1);**

成功了

但我知道这不会在其他电脑上工作,所以我想知道如何编写可以在任何电脑上运行的相同代码

我也试过这样

(@"pack://application:,,,/Widne;component/help/mypdf.pdf" 

但它不起作用

[更新]

我已经尝试了所有的解决方案,但仍然无法正常工作,但我不知道为什么? 请再检查一次 Widne >> 帮助 >> mypdf

【问题讨论】:

  • 你的 .exe 在哪个文件夹?
  • 你的exe目录下的Widne ?
  • 检查我的更新答案@Akrem
  • 这个目录会存在于你的调试目录中吗?这个目录在你的主项目中吗?
  • 不,我没有向您展示调试目录,是的,此目录在主项目中

标签: c# wpf


【解决方案1】:

使用你的exe的路径

System.AppDomain.CurrentDomain.BaseDirectory

string path = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
             + "\\Widne\\component\\help\\mypdf.pdf";

System.Diagnostics.Process.Start(path )

更新:

在打开文件之前,他必须存在,在这种情况下,您将属性设置为您的 PDF 文件 Copy to Output DirectoryCopy always

【讨论】:

  • 检查更新我不知道为什么它仍然无法正常工作
  • @dholakiyaankit 我更新我的答案检查并回复
【解决方案2】:

第二种方法packuri是你应该使用的。

您的问题很可能是您的mypdf.pdf 需要在 Visual Studio 中更改其属性,以便它在构建时实际复制 pdf 文件,而不是嵌入到应用程序中,从而阻止最终用户读取 pdf 文件。

在 pdf 文件上设置这些属性并重建应用程序

构建操作:内容
复制到输出目录:总是复制

编辑:查看 this answer 以了解构建操作的说明。

【讨论】:

  • 请多解释一下
  • 检查更新我不知道为什么它仍然无法正常工作
  • 谢谢,我只是无法得到你的全部
【解决方案3】:

Diagnostics.Processclass 与 WPF 无关。如果路径是相对于您的应用程序的,您可以使用相对路径。如果您担心应用程序在与存储程序集的目录不同的工作目录中运行,您可以使用查询该程序集的原始文件夹

 Assembly.GetExecutingAssembly().CodeBase

并使用Path.Combine 构造 PDF 的路径。

【讨论】:

  • 检查更新我不知道为什么它仍然无法正常工作
  • 您需要获取目录名称 - CodeBase 为您提供 .exe 文件。 string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"PrintTemplates\Promotions\images\promotion1.png");
【解决方案4】:

你可以使用相对路径:

./ - start at the .exe directory

../ - go up one folder from the .exe

../../ - go up two folders etc.

编辑:

假设你的 .exe 在 bin 文件夹中:

**string pdfurl1 = ((@"..\Help\Emerald Wine Dispensing Software.pdf"));
                    System.Diagnostics.Process.Start(pdfurl1);**

【讨论】:

  • 检查更新我不知道为什么它仍然无法正常工作
  • @dholakiyaankit 查看编辑。你的 .exe 在哪里?
【解决方案5】:

您可以使用Directory.GetCurrentDirectory()。这将为您提供启动应用程序的目录。因此,如果您将文件放在启动应用程序的位置,它将是这样的:

string pdfurl1 = ((Directory.GetCurrentDirectory() + "\Emerald Wine Dispensing Software.pdf"));
System.Diagnostics.Process.Start(pdfurl1);

【讨论】:

  • 检查更新我不知道为什么它仍然无法正常工作
【解决方案6】:

也许here的回答可以帮到你

使用Path 类来构建您的路径。它会做正确的事。

对包含文件或目录的 String 实例执行操作 路径信息。这些操作在跨平台中执行 方式。

var full = Path.Combine(baseDir, dirFragment);

【讨论】:

    【解决方案7】:

    对于Directory Dialog获取Directory Path,首先Add引用System.Windows.Forms,然后Resolve,然后把这段代码放在一个按钮点击。

            var dialog = new FolderBrowserDialog();
            dialog.ShowDialog();
            folderpathTB.Text = dialog.SelectedPath;
    

    (folderpathTB 是 TextBox 的名称,我想在其中放置文件夹路径,或者您也可以将其分配给字符串变量,即)

            string folder = dialog.SelectedPath;
    


    如果你想获得文件名/路径,只需在按钮单击上执行此操作

            FileDialog fileDialog = new OpenFileDialog();
            fileDialog.ShowDialog();
            folderpathTB.Text = fileDialog.FileName;
    

    (folderpathTB 是文本框的名称,我想在其中放置文件路径,或者您也可以将其分配给字符串变量)

    注意:对于文件夹对话框,必须将 System.Windows.Forms.dll 添加到项目中,否则将不起作用。

    【讨论】:

      【解决方案8】:

      你也可以试试这个代码:

      string pdfFolder;
      
      #if DEBUG
                  DirectoryInfo directoryInfo = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).FullName);
                  pdfFolder = directoryInfo.FullName + @"\PdfFile\Auto Refill Reminder List.pdf";
                  System.Diagnostics.Process.Start(pdfFolder );
      #endif
      
      #if (!DEBUG)
                  pdfFolder = Environment.CurrentDirectory + @"\PdfFile\Auto Refill Reminder List.pdf";
                  Process.Start(pdfFolder);
      #endif
      

      并更改内容的构建类型:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-06
        • 2013-06-16
        • 1970-01-01
        • 2017-11-03
        相关资源
        最近更新 更多