मुझे ASP.NET का उपयोग करने वाले फ़ोल्डर से C # के साथ फ़ाइलों की संख्या कैसे मिलती है?
मुझे ASP.NET का उपयोग करने वाले फ़ोल्डर से C # के साथ फ़ाइलों की संख्या कैसे मिलती है?
जवाबों:
System.IO.Directory myDir = GetMyDirectoryForTheExample();
int count = myDir.GetFiles().Length;
आप Directory.GetFiles विधि का उपयोग कर सकते हैं
इसके अलावा Directory.GetFiles विधि (स्ट्रिंग, स्ट्रिंग, SearchOption) देखें
आप इस अधिभार में खोज विकल्प निर्दिष्ट कर सकते हैं।
TopDirectoryOnly : किसी खोज में केवल वर्तमान निर्देशिका शामिल है।
AllDirectories : एक खोज ऑपरेशन में वर्तमान निर्देशिका और सभी उपनिर्देशिकाएं शामिल हैं। इस विकल्प में खोज में आरोहित बिंदु जैसे माउंटेड ड्राइव और प्रतीकात्मक लिंक शामिल हैं।
// searches the current directory and sub directory
int fCount = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length;
// searches the current directory
int fCount = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly).Length;
GetFileबनाने के लिए स्ट्रिंग [] जिसमें फ्लैट मेमोरी स्पेस की आवश्यकता होती है। सावधान रहें :)
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("SourcePath");
int count = dir.GetFiles().Length;
आप इसका उपयोग कर सकते हैं।
एक निर्देशिका से पीडीएफ फाइलों को पढ़ना:
var list = Directory.GetFiles(@"C:\ScanPDF", "*.pdf");
if (list.Length > 0)
{
}
.NET मेथड्स Directory.GetFiles (dir) या DirectoryInfo.GetFiles () सिर्फ एक कुल फाइल काउंट प्राप्त करने के लिए बहुत तेज नहीं हैं। यदि आप इस फ़ाइल गणना पद्धति का बहुत अधिक उपयोग करते हैं , तो सीधे WinAPI का उपयोग करने पर विचार करें , जो लगभग 50% समय बचाता है।
यहाँ WinAPI दृष्टिकोण है जहाँ मैं WinAPI कॉल्स को C # विधि में संलग्न करता हूँ:
int GetFileCount(string dir, bool includeSubdirectories = false)
पूरा कोड:
[Serializable, StructLayout(LayoutKind.Sequential)]
private struct WIN32_FIND_DATA
{
public int dwFileAttributes;
public int ftCreationTime_dwLowDateTime;
public int ftCreationTime_dwHighDateTime;
public int ftLastAccessTime_dwLowDateTime;
public int ftLastAccessTime_dwHighDateTime;
public int ftLastWriteTime_dwLowDateTime;
public int ftLastWriteTime_dwHighDateTime;
public int nFileSizeHigh;
public int nFileSizeLow;
public int dwReserved0;
public int dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}
[DllImport("kernel32.dll")]
private static extern IntPtr FindFirstFile(string pFileName, ref WIN32_FIND_DATA pFindFileData);
[DllImport("kernel32.dll")]
private static extern bool FindNextFile(IntPtr hFindFile, ref WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll")]
private static extern bool FindClose(IntPtr hFindFile);
private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
private const int FILE_ATTRIBUTE_DIRECTORY = 16;
private int GetFileCount(string dir, bool includeSubdirectories = false)
{
string searchPattern = Path.Combine(dir, "*");
var findFileData = new WIN32_FIND_DATA();
IntPtr hFindFile = FindFirstFile(searchPattern, ref findFileData);
if (hFindFile == INVALID_HANDLE_VALUE)
throw new Exception("Directory not found: " + dir);
int fileCount = 0;
do
{
if (findFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
{
fileCount++;
continue;
}
if (includeSubdirectories && findFileData.cFileName != "." && findFileData.cFileName != "..")
{
string subDir = Path.Combine(dir, findFileData.cFileName);
fileCount += GetFileCount(subDir, true);
}
}
while (FindNextFile(hFindFile, ref findFileData));
FindClose(hFindFile);
return fileCount;
}
जब मैं अपने कंप्यूटर पर 13000 फ़ाइलों के साथ एक फ़ोल्डर में खोजता हूं - औसत: 110ms
int fileCount = GetFileCount(searchDir, true); // using WinAPI
.NET निर्मित विधि: Directory.GetFiles (dir) - औसत: 230ms
int fileCount = Directory.GetFiles(searchDir, "*", SearchOption.AllDirectories).Length;
नोट: दोनों विधियों में से पहला रन क्रमशः 60% - 100% धीमा होगा क्योंकि हार्ड ड्राइव को सेक्टरों का पता लगाने में थोड़ा अधिक समय लगता है। मुझे लगता है कि विंडोज के बाद आने वाली कॉल अर्ध-कैश्ड होगी।
int fileCount = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length; // Will Retrieve count of all files in directry and sub directries
int fileCount = Directory.GetFiles(path, "*.*", SearchOption.TopDirectory).Length; // Will Retrieve count of all files in directry but not sub directries
int fileCount = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories).Length; // Will Retrieve count of files XML extension in directry and sub directries
int filesCount = Directory.EnumerateFiles(Directory).Count();