简体   繁体   English

打开目录文件夹中的文件

[英]Open files in a directory folder

[EDIT] [编辑]

I try to open a file from a folder in my base directory. 我尝试从基本目录中的文件夹打开文件。

From the solution explorer, there is a folder "Docs" and a pdf file in it (tempPDF.pdf). 在解决方案资源管理器中,有一个文件夹“ Docs”和一个pdf文件(tempPDF.pdf)。 That will corresponds to a folder in my Project Folder. 那将对应于我的项目文件夹中的一个文件夹。 (eg Project\\Docs) this is in the save level as bin and properties folders. (例如Project \\ Docs),这在保存级别为bin和properties文件夹中。

String assemblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);

Process proc = new Process();
proc.StartInfo = new ProcessStartInfo()
{
    FileName = assemblyPath + "\\Docs\\tempPDF.pdf"
};
proc.Start();

I have already changed the Build Action to "Resource" and Copy to Output Directory to "Copy Always". 我已经将“ 生成操作 ”更改为“资源”,并将“ 复制到输出目录 ”更改为“始终复制”。 Yet, the pdf file is not copies to the bin, nor the file can be specified by the program. 但是,pdf文件不会复制到垃圾箱,程序也无法指定该文件。 I clean, build and rebuild the solution. 我清理,构建和重建解决方案。

Path kept pointing to bin\\Debug folder because this is where the executables is located but the files are location at the same level as bin. 路径始终指向bin \\ Debug文件夹,因为这是可执行文件所在的位置,但是文件位于与bin相同级别的位置。

I can simply specific my image in XAML by a path like "Images\\img.png", and it will find the image relatively. 我可以简单地通过“ Images \\ img.png”之类的路径在XAML中指定我的图像,它将相对地找到该图像。

This is supposed to be a very simple question - open a PDF from a mouse click. 这应该是一个非常简单的问题-单击鼠标即可打开PDF。

[EDIT - with answer] [编辑-有答案]

Have to change Build Action to "Embedded Resource" , NOT "Resource" (Resource only copy a empty folder without the files). 必须将“ 构建操作 更改为“嵌入式资源” ,而不是“资源” (资源仅复制一个空文件夹而不包含文件)。 I don't know why though. 我不知道为什么。

First, there is no XAML solution for that. 首先,没有针对此的XAML解决方案。 We cannot execute a process without code-behind assistance. 没有代码支持,我们无法执行流程。

The URI you show is relative to the current assembly. 您显示的URI是相对于当前程序集的。 You can use the assembly directory path then append the relative path of the file. 您可以使用程序集目录路径,然后附加文件的相对路径。 It can be something like this: 可能是这样的:

String assemblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);

Process proc = new Process();
proc.StartInfo = new ProcessStartInfo()
{
    FileName = assemblyPath + "\\Docs\\somePDF.pdf"
};
proc.Start();

If you want to put your documents in the application folder, you can also use that directory path instead of the assembly path: 如果要将文档放在应用程序文件夹中,也可以使用该目录路径而不是程序集路径:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

You can get assembly path as Nadzzz wrote. 您可以按照Nadzzz的说明获得组装路径。 If you want to get '...bin' path instead of '...bin\\debug' you can use Directory.GetParent method. 如果要获取“ ... bin”路径而不是“ ... bin \\ debug”,则可以使用Directory.GetParent方法。

static function to open the folder a file is located in.  
I use in a static common class for many of my projects.

public static void ShowFileInFolder(string filepath)
{
        System.Diagnostics.Process prc = new System.Diagnostics.Process();
        prc.StartInfo.FileName = Path.GetDirectoryName(filepath);
        prc.Start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM