C
C#7mo ago
js

directory.getfiles help

in the photo is my project folder, i need to get all the files in the TestData folder, previously i used this code but if I run it on another machine it wont work as directory will be different
string[] filePaths = Directory.GetFiles(@"C:\Users\Joe\Documents\Uni\Programming Portfolio\JustNom\JustNom\TestData", "*.nom");
string[] filePaths = Directory.GetFiles(@"C:\Users\Joe\Documents\Uni\Programming Portfolio\JustNom\JustNom\TestData", "*.nom");
I want it to shorten the directory so it just points to the testdata folder within the project, how would i do this
No description
8 Replies
SpReeD
SpReeD7mo ago
Don't use an hardcoded and absolute path.
js
jsOP7mo ago
yes i know ive seen people use a short way just pointing to the folder in the project folder how do i write that
SpReeD
SpReeD7mo ago
Instead, build one dynamically, using Path.GetFullPath(Path.Combine("..","..","..", Evironment.CurrentPath)); something like it. Here's an example
string testFile = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "../../../", "TestFiles", "testFile.pdf"));
string testFile = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "../../../", "TestFiles", "testFile.pdf"));
js
jsOP7mo ago
ok thanks bro
Pobiega
Pobiega7mo ago
a much better solution would be to set up <CopyToOutputDirectory> for the files you want copied, then just pass a relative path without any .. this would make your program work fine even when published, and not just when running from inside visual studio
maxmahem
maxmahem7mo ago
welll the easy answer is of course to just change that path. oh doh scrolled up there is also a method to get the special folder path, but Pobiega's answere is the right one
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
SpReeD
SpReeD7mo ago
That's wrong, it does work if the structure is persistent, which in the case of testfiles is absolutely fine. Besides that, the question was about paths, and knowing that there's a way to get the actual path by moving higher in hierarchy by using .. is also legit. I'm not saying it's the only solution, it's one of many, like always, it depends on the use-case.
Want results from more Discord servers?
Add your server