How to give links of files from folder.

Abdul Hayee

Member
Joined
Mar 31, 2020
Messages
24
Programming Experience
Beginner
Hi
I want to open a pdf file when i click a particular button/link.
Currently i have done this by placing the pdf file in my project folder. like as
1589716268653.png

1589716150187.png

and code for this is
1589716323340.png

This is working fine.

Now what i want is to place all the pdf files in a folder name "documents" which is inside the project folder
1589716424308.png


My question is what should i change in the code to open a file from document folder?
1589716545819.png



I am doing this because first my files will be seperate from project files and secondly when i create exe file for the project all these pdf files will be included in the project.

Thanks.
 
In the future, please do do screenshots of your code. Actually paste the code code inside code tags.

Anyway, if you want to just open the the documents folder using the Windows File Explorer so that the user can browse around in it, then the easiest way is to do something like:
C#:
string folderName = Path.Combine(Environment.CurrentDirectory, "documents");
Process.Start($@"explorer.exe ""{folderName}""");

Now on the other hand, if you actually want to open each of the documents found within the documents folder, then you'll need to use Directory.GetFiles() to get a list of files, and then start a process to with each of the filenames that you get back. In pseudo-code:
C#:
foreach(var file in Directory.GetFiles("documents"))
    Process.Start(file)
 
Thanks SkyDiver
Thanks for your reply
Kindly clear me some points
In your first code
1- The "explorer.exe" is the file which we want to open? like if i have pdf file, i write its name here. for example file1.pdf
2- ""{foldername}"" , what does this describes? should i write it as it is or i have to add something else?

In your second code
1- Process.Start(file), how does it works? i mean if i want to open file1.pdf then how it will open that file?

Thanks
 
"explorer.exe" is the the Windows File Explorer process. When you start "explorer.exe" and pass it the name of file, it will try to show you a view of that folder. Give it a try: open a CMD window, and type in "explorer.exe C:\Windows\System32" and it should show you a view of your C:\Windows\System32 folder.

So there is a little bit of magic when you call Process.Start() and how it processes what you pass in. You can read about in the documentation:
Starting a process by specifying its file name is similar to typing the information in the Run dialog box of the Windows Start menu. Therefore, the file name does not need to represent an executable file. It can be of any file type for which the extension has been associated with an application installed on the system. For example the file name can have a .txt extension if you have associated text files with an editor, such as Notepad, or it can have a .doc if you have associated .doc files with a word processing tool, such as Microsoft Word. Similarly, in the same way that the Run dialog box can accept an executable file name with or without the .exe extension, the .exe extension is optional in the fileName parameter. For example, you can set the fileName parameter to either "Notepad.exe" or "Notepad".

Also, on reading the documentation, I gave you bad advice of using this approach:
C#:
string folderName = Path.Combine(Environment.CurrentDirectory, "documents");
Process.Start($@"explorer.exe ""{folderName}""");
as per the documentation, it should have been this instead:
C#:
string folderName = Path.Combine(Environment.CurrentDirectory, "documents");
Process.Start("explorer.exe", folderName);
using two parameters instead of just a single string.
 
Back
Top Bottom