开发者

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). That will corresponds to a folder in my Project Folder. (e.g. Project\Docs) this is in the save level as bin and properties folders.

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. 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.

I can simply specific my image in XAML by a path like "Images\img.png", and it will find the image relatively.

This is supposed to be a very simple question - open a PDF from a mouse click.

[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. We cannot execute a process without code-behind assistance.

The URI you show is relative to the current assembly. 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. If you want to get '...bin' path instead of '...bin\debug' you can use Directory.GetParent method.


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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜