Get files from user folder inside c # project

The first poster (and the newbie).

I created a winform application for C #. I added the Documents folder, in which I added 5 PDF files.

In my Form1, I added a button and inside the button click event, I try to get files from this Documents folder.

I googled around and found something like this:

string[] arr = Directory.GetFiles(string path); 

But I do not want to "hard code" the path to the "Documents" folder. I would like to know if there is a way (more dynamic) to get the path to the Documents folder.

I also found them:

 string path1 = Path.GetDirectoryName(Application.ExecutablePath); string path2 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 

But they always bring me to the \ bin \ Debug folder.

I will take all the help I can get! Thanks!

+6
source share
2 answers

Environment.SpecialFolder Do you mean enumeration?

 Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) 

Or am I misunderstanding the question?

EDIT

I guess I misunderstood my apologies. Try the following:

 string documents = Path.Combine( Path.GetDirectoryName(Application.ExecutablePath), "Documents" ); 

It also assumes that you include items from the documents folder as resources so that the executable can see them.

+9
source

You will either have to tell Visual Studio to copy the files for output, or, as I would do, they will be in% APPDATA%

0
source

Source: https://habr.com/ru/post/890147/


All Articles