मैंने सफलतापूर्वक एक वर्ग बनाया है जिसमें एक विधि है जो किसी दिए गए निर्देशिका में आइटम पथों की एक सूची लौटाती है।
समस्या यह है, खोज मानदंड केवल खोज परिणाम लौटाते हैं जिसमें प्रारंभ में उपयोगकर्ता का इनपुट शामिल होता है।
मैं जो चाहता हूं वह यह है कि खोज परिणामों में कोई भी आइटम शामिल होगा जिसमें आइटम की स्ट्रिंग के भीतर कहीं भी उपयोगकर्ता का इनपुट होता है, न कि यह उपयोगकर्ता द्वारा प्रदान किए गए इनपुट से शुरू होता है।
यहाँ स्रोत कोड है:
public class SearchManager
{
//Returns a list of items that contain the user's input within a given path
public static List<string> SearchResults(string current_path, string user_input)
{
//Here we access the class 'DirectoryInfo'
DirectoryInfo dir_info = new DirectoryInfo(current_path);
//This array stores all the directories found within the current path/directory
DirectoryInfo[] directories = dir_info.GetDirectories(user_input + "*", SearchOption.TopDirectoryOnly);
//This array stores all the files found within the current path/directory
FileInfo[] files = dir_info.GetFiles(user_input + "*", SearchOption.TopDirectoryOnly);
//This list will store both directories and files found within the search result
List<string> ItemsFound = new List<string>();
//Here we loop through each item within both arrays in order to populate that list of items
//Adds all the given paths of the directories
foreach (DirectoryInfo dir in directories)
ItemsFound.Add(dir.FullName);
//Adds all the given paths of the files
foreach (FileInfo file in files)
ItemsFound.Add(file.FullName);
//Here, we return a list of items data, from our search result to populate the data grid
return ItemsFound;
}
मैं इस मुद्दे को कैसे हल करूं? धन्यवाद! :)
2 जवाब
आप शुरुआत में बस इस तरह से एक वाइल्डकार्ड जोड़ सकते हैं:
//This array stores all the directories found within the current path/directory
DirectoryInfo[] directories = dir_info.GetDirectories("*" + user_input + "*", SearchOption.TopDirectoryOnly);
//This array stores all the files found within the current path/directory
FileInfo[] files = dir_info.GetFiles("*" + user_input + "*", SearchOption.TopDirectoryOnly);
इसके अलावा, आप अपनी पूरी विधि को छोटा करने के लिए linq का उपयोग कर सकते हैं।
public static List<string> SearchResults(string current_path, string user_input)
{
//Here we access the class 'DirectoryInfo'
DirectoryInfo dir_info = new DirectoryInfo(current_path);
//This array stores all the directories found within the current path/directory
DirectoryInfo[] directories = dir_info.GetDirectories("*" + user_input + "*", SearchOption.TopDirectoryOnly);
//This array stores all the files found within the current path/directory
FileInfo[] files = dir_info.GetFiles("*" + user_input + "*", SearchOption.TopDirectoryOnly);
//This list will store both directories and files found within the search result
List<string> ItemsFound = new List<string>(); ;
ItemsFound.AddRange(directories.Select(x => x.FullName));
ItemsFound.AddRange(files.Select(x => x.FullName));
//Here, we return a list of items data, from our search result to populate the data grid
return ItemsFound;
}
या dir_info को linq में जोड़कर भी छोटा:
public static List<string> SearchResults(string current_path, string user_input)
{
//Here we access the class 'DirectoryInfo'
DirectoryInfo dir_info = new DirectoryInfo(current_path);
//This list will store both directories and files found within the search result
List<string> ItemsFound = new List<string>();
ItemsFound.AddRange(dir_info.GetDirectories("*" + user_input + "*", SearchOption.TopDirectoryOnly).Select(x => x.FullName));
ItemsFound.AddRange(dir_info.GetFiles("*" + user_input + "*", SearchOption.TopDirectoryOnly).Select(x => x.FullName));
//Here, we return a list of items data, from our search result to populate the data grid
return ItemsFound;
}
या इससे भी छोटा, लेकिन मैं ऐसा करने की अनुशंसा नहीं करता (मैं अभी ठीक हूं और अभी कुछ नहीं करना है)
public static List<string> SearchResults(string current_path, string user_input)
=> (new DirectoryInfo(current_path)).GetFiles("*" + user_input + "*", SearchOption.TopDirectoryOnly).Select(x => x.FullName).Concat(((new DirectoryInfo(current_path)).GetDirectories("*" + user_input + "*", SearchOption.TopDirectoryOnly).Select(x => x.FullName))).ToList();
सर्चपैटर्न बदलें = *userinput*
DirectoryInfo[] directories = dir_info.GetDirectories("*" + user_input + "*", SearchOption.TopDirectoryOnly);
//This array stores all the files found within the current path/directory
FileInfo[] files = dir_info.GetFiles("*" + user_input + "*", SearchOption.TopDirectoryOnly);
नए सवाल
c#
C # (उच्चारण "तेज देखें") Microsoft द्वारा विकसित एक उच्च स्तरीय, सांख्यिकीय रूप से टाइप किया हुआ, बहु-प्रतिमान प्रोग्रामिंग भाषा है। C # कोड आमतौर पर Microsoft के .NET परिवार के टूल और रन-टाइम को लक्षित करता है, जिसमें .NET फ्रेमवर्क, .NET कोर और Xamarin अन्य शामिल हैं। C # या C # के औपचारिक विनिर्देश में लिखे गए कोड के बारे में प्रश्नों के लिए इस टैग का उपयोग करें।