简体   繁体   中英

How to get file path from folder wpf?

I am having an issue, I am developing a WPF application where I have one page named as "help" having one button go When user clicks on this button I have to provide pdf file to the user which is in the help folder. Now my problem is if I write path like this

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

Its working successfully

but I know this will not going to work in other pcs so I want to know how can I write code for the same that can be run on any pcs

I have also tried like this

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

But its not working

[Updated]

I have tried all the solutions but its not working yet I don't know why? please check once again Widne >> Help >> mypdf

在此处输入图片说明

use the path of your 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 )

Update :

befor opning the file he must be existing, in this case you make the property to your PDF file Copy to Output Directory to Copy always

The 2nd method packuri is what you should use.

Your problem is most likely that your mypdf.pdf needs to have it's properties changed in Visual Studio so that it actually copies the pdf file when built and not embed in the application preventing the end-user from reading the pdf file.

Set these properties on the pdf file and rebuild the application

Build Action: Content
Copy to Output Directory: Copy always

Edit: Look at this answer for an explanation of Build Action.

You could use a relative path:

./ - start at the .exe directory

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

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

EDIT:

Assuming your .exe in in the bin folder:

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

The Diagnostics.Process class has nothing to do with WPF. If the path is relative to your application, you can use relative paths. If you fear that the application is run with a working directory different to the directory where the assembly is stored, you can query the original folder of that assembly using

 Assembly.GetExecutingAssembly().CodeBase

and construct the path to the PDF with Path.Combine .

You can use Directory.GetCurrentDirectory() . That will give you the directory from where the application was launched. So if you place your file where the application was launched it would be something like this:

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

Maybe the answer from here can help you

Use the Path class to build up your paths. It will do the right thing.

Performs operations on String instances that contain file or directory path information. These operations are performed in a cross-platform manner.

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

For Directory Dialog to get the Directory Path, First Add reference System.Windows.Forms, and then Resolve, and then put this code in a button click.

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

(folderpathTB is name of TextBox where I wana put the folder path, OR u can assign it to a string variable too ie)

        string folder = dialog.SelectedPath;


And if you wana get FileName/path, Simply do this on Button Click

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

(folderpathTB is name of TextBox where I wana put the file path, OR u can assign it to a string variable too)

Note: For Folder Dialog, the System.Windows.Forms.dll must be added to the project, otherwise it wouldn't work.

You can also try with this code:

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

And change the build type of your content:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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