C#使用日期范围选项计算服务器目录中的文件 [英] C# Count files in server directory with option of date range
本文介绍了C#使用日期范围选项计算服务器目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用C#计算共享文件夹中的文件,但是我们需要知道一月份的数量,例如10月份的飞蛾。手动计数可以有很多。
这是我必须计算文件夹位置中所有文件的代码:
< pre lang = cs >使用System;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.IO;
命名空间 FileCount
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo( @ \\\\server\path);
int count = dir.GetFiles()。Length;
string scount = count.ToString();
label1.Text = scount;
解决方案
我使用的方法如下:// 必需
使用 System.Collections.Generic;
使用 System.IO;
使用 System.Linq;
private List< FileInfo> GetFilesByMonth( int monthToGet, string directoryPath)
{
DirectoryInfo dir = new DirectoryInfo(directoryPath);
// 注意使用搜索子目录的选项
FileInfo [] theFiles = dir.GetFiles( *,SearchOption.AllDirectories);
return theFiles.Where(fl = > fl.CreationTime.Month == monthToGet)。ToList();
}可以这样调用:
List< FileInfo> FilesInNovember = GetFilesByMonth( 11 , @ \\ \\server\path跨度>);
I can count files in a shared folder with C# but we are needing to know the amount for a month at a time like the moth of October. There is to many to manually count manually.
Here is the code I have to count all files in the folder location:
<pre lang="cs">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FileCount
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"\\\\server\path");
int count = dir.GetFiles().Length;
string scount = count.ToString();
label1.Text = scount;
解决方案
I'd use a method like:// required using System.Collections.Generic; using System.IO; using System.Linq; private List<FileInfo> GetFilesByMonth(int monthToGet, string directoryPath) { DirectoryInfo dir = new DirectoryInfo(directoryPath); // note use of the option to search sub-directories FileInfo[] theFiles = dir.GetFiles("*", SearchOption.AllDirectories); return theFiles.Where(fl => fl.CreationTime.Month == monthToGet).ToList(); }Which can be called like this:
List<FileInfo> FilesInNovember = GetFilesByMonth(11, @"\\\\server\path");
这篇关于C#使用日期范围选项计算服务器目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文