如何将日期时间选择器值与某些文件夹名称进行比较

发布于 2024-12-09 09:02:32 字数 398 浏览 0 评论 0原文

我的文件夹路径中有一个备份文件,如下所示 C:\Folder

文件如下图所示在此处输入图像描述

文件名像这样...20111011 表示今天日期 095523 表示时间

,我有一个日期时间选择器,当用户在日期时间选择器中选择值并选择按钮时,我有按钮,

我需要将日期时间选择器值与存储在该文件夹中的文件名进行比较,然后如果日期时间选择器值与文件夹名称的部分相匹配(backup-{this is part}) 我想将zip文件提取到给定文件夹中....

我怎样才能将日期时间选择器值与文件夹名称和将文件解压到给定路径....

I have a backup files in folder path is like this C:\Folder

the files are like this in below imageenter image description here

the file name like this...20111011 means today date
095523 means time

and i have a date time picker and i have button when the user select the value in datetime picker and select the button ,

I need to compare the date time picker value with the file name stored in that folder , and then if date time picker value matches the part of the folder name (backup-{this is part}) i want to extract zip file into the given folder ....

how can i get compare date time picker value with the folder name and extract the files in to given path ....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一人独醉 2024-12-16 09:02:32

在.net Directory.GetFiles 方法中,用于从特定位置获取文件小路。

下面链接中提到的代码根据需要在 foreach 循环中进行更改。这个循环给你文件名,你可以比较它。

public static void ProcessDirectory(string targetDirectory) 
{
    // Process the list of files found in the directory.
    string [] fileEntries = Directory.GetFiles(targetDirectory);
    foreach(string fileName in fileEntries)
      Do work here which you need.
}

检查此链接以获取 datetimepicker 值

In .net Directory.GetFiles method used to get files from specific path.

Below code mentioned in link make changes in foreach loop as you need. This loop give you filename and you can compare it.

public static void ProcessDirectory(string targetDirectory) 
{
    // Process the list of files found in the directory.
    string [] fileEntries = Directory.GetFiles(targetDirectory);
    foreach(string fileName in fileEntries)
      Do work here which you need.
}

Check this link for datetimepicker value

旧瑾黎汐 2024-12-16 09:02:32

您可以使用 System.IO.File

阅读完它们后,您可以使用 DateTime.ParseExact 到解析文件日期和时间。

然后您只需将文件日期和时间与日期选择器的值进行比较即可。

You can read the file names using System.IO.File

When you have read them, you can extract their dates using DateTime.ParseExact to parse the file date and time.

And then you just have to compare the file date and time with the value of your date picker.

甜中书 2024-12-16 09:02:32

您可以尝试这样的操作:

DateTime dateSelected = <date coming from the picker>;
string fileRequested = string.Format("backup-{0}{1}{2}{3}{4}{5}",dateSelected.Year,dateSelected.Month,dateSelected.Day,dateSelected.Hour,dateSelected.Minute,dateSelected.Second;

然后您可以使用 Emaad 的答案来处理该文件。

You can try something like this:

DateTime dateSelected = <date coming from the picker>;
string fileRequested = string.Format("backup-{0}{1}{2}{3}{4}{5}",dateSelected.Year,dateSelected.Month,dateSelected.Day,dateSelected.Hour,dateSelected.Minute,dateSelected.Second;

Then you can use the answer from Emaad to get work with the file.

百善笑为先 2024-12-16 09:02:32

只是有点“hacky”——但它确实有效。

        var dateToParse = "20111011095323";
        var date = new DateTime(int.Parse(dateToParse.Substring(0, 4)),
                                int.Parse(dateToParse.Substring(4, 2)),
                                int.Parse(dateToParse.Substring(6, 2)),
                                int.Parse(dateToParse.Substring(8, 2)),
                                int.Parse(dateToParse.Substring(10, 2)),
                                int.Parse(dateToParse.Substring(12, 2)));

Just a it "hacky" - but it works.

        var dateToParse = "20111011095323";
        var date = new DateTime(int.Parse(dateToParse.Substring(0, 4)),
                                int.Parse(dateToParse.Substring(4, 2)),
                                int.Parse(dateToParse.Substring(6, 2)),
                                int.Parse(dateToParse.Substring(8, 2)),
                                int.Parse(dateToParse.Substring(10, 2)),
                                int.Parse(dateToParse.Substring(12, 2)));
复古式 2024-12-16 09:02:32

首先,您必须设置 DateTimePicker正确的日期时间格式以便选择日期和时间。

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM dd yyyy hh mm ss";  

接下来我认为您必须使用 DateTimePicker.Value 构造文件名。请查看此处 ,您可以将 DateTime ToString 转换为您所需的格式。

例如...

DateTime dt = dateTimePicker1.Value;
string fileName = string.Format("backup-{0}",dt.ToString(yyyyMMddHHmmss));

不确定您的时间格式。如果是 12 小时则使用“H”,如果是 24 小时则使用“HH”。请参阅此处了解更多示例。剩下的就不难了...

First of all, you will have to set the DateTimePicker date-time format properly in order to select both date and time.

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM dd yyyy hh mm ss";  

Next I think you have to construct the file name by using the DateTimePicker.Value. Look at here, you can ToString the DateTime to your required format.

For example...

DateTime dt = dateTimePicker1.Value;
string fileName = string.Format("backup-{0}",dt.ToString(yyyyMMddHHmmss));

Not sure about your time format. If it is 12 hour use "H" and use "HH" for 24 hours. Look at here for more examples. The rest won't be difficult...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文