Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
the name of the question was not entirely chosen, but I did not know how to summarize it in a short version. So the question is, how to find the file (s) by part of the name? Then to sort only the searched extension (csproj) from the found files?
Then I would like to rewrite something in these files, but that would be for another time. I'm trying to create it in a WPF application in c #.

I will be happy for any advice.

Thank you.

What I have tried:

C#
public partial class MainWindow : Window
   {
       public MainWindow()
       {
           InitializeComponent();
       }

       private void Grid_Loaded(object sender, RoutedEventArgs e)
       {
           TextBox.Focus();
       }

       private void HledatButton_Click(object sender, RoutedEventArgs e)
       {
           string file = TextBox.Text;
           string[] files = Directory.GetFiles(file,"*.csproj", SearchOption.AllDirectories);
           Console.WriteLine(files);

       }

   }

This code only searches the file the application is in, how do I search other directories?
Posted
Updated 4-Nov-21 23:05pm
v2

Chances are, it only searches in the root directory. Try
C#
FileInfo[] filesInDir = hdDirectoryInWhichToSearch.GetFiles
(
   $"*{file}*.csproj",
   EnumerationOptions.RecurseSubdirectories
);
 
Share this answer
 
Comments
dejf111 5-Nov-21 4:17am    
RecurseSubdirectories is not working
phil.o 5-Nov-21 4:20am    
Then it's time to lauch a debug session. Put a breakpoint on the first line of the click event handler, press F5, and investigate from here.
Note that accessing root directory is not recommended, and even prohibited on modern OSes. Try to browse from a subdirectory instead.
Try this:
C#
List<string> files = new DirectoryInfo(@"C:\TopLevelFolderPath").EnumerateFiles("SH*.*", SearchOption.AllDirectories).Select(d => d.FullName).ToList();
That will find all files starting with "SH" in the TopLEvelFolder or a subdirectory.
 
Share this answer
 
Comments
dejf111 5-Nov-21 4:34am    
in case I want to search all files on all disks?
OriginalGriff 5-Nov-21 5:00am    
All disks have independant file systems - heck, they could even be a mixture of FAT, FAT16, FAT32, NTFS, ... the list goes on.

So no matter what you use, you would need to check each disk separately - there is no standard code for finding a file "anywhere my PC can reach"! :laugh:
dejf111 5-Nov-21 5:01am    
That's quite a shame :laugh:
Dave Kreskowiak 5-Nov-21 8:28am    
Quite a shame? If you have, say, 7 network drives mapped, covering about 200TB of data, how long would you think it would take to search all of those drives? It would take a R E A L L Y long time.

That's why you DON'T have a solution to search all drives with one method call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900