例用C# 变更文件夹时间

简介:
想变更文件夹的时间.以前做了一个软件.是变更文件的时间,应用了C# API.这次和上一次相同,用Form来通过外部对时间进行确认.确认后应用C#中,System.IO ->Directory->SetCreationTime 来对时间进行变更.
以下是C#应用的例子.基本相同.但是,不要忘记小问题,下面的例子的命名空间,和函数的调用.
C#
// This sample shows the differences between dates from methods that use
//coordinated universal time (UTC) format and those that do not.
using System;
using System.IO;
namespace IOSamples
{
  public class DirectoryUTCTime
  {
    public static void Main()
    {
    // Set the directory.
      string n = @"C:\test\newdir";
        //Create two variables to use to set the time.
      DateTime dtime1 = new DateTime(2002, 1, 3);
      DateTime dtime2 = new DateTime(1999, 1, 1);
    //Create the directory.
      try
      {
          Directory.CreateDirectory(n);
      }
      catch (IOException e)
      {
          Console.WriteLine(e);
      }
    //Set the creation and last access times to a variable DateTime value.
      Directory.SetCreationTime(n, dtime1);
      Directory.SetLastAccessTimeUtc(n, dtime1);
        // Print to console the results.
      Console.WriteLine("Creation Date: {0}", Directory.GetCreationTime(n));
      Console.WriteLine("UTC creation Date: {0}", Directory.GetCreationTimeUtc(n));
      Console.WriteLine("Last write time: {0}", Directory.GetLastWriteTime(n));
      Console.WriteLine("UTC last write time: {0}", Directory.GetLastWriteTimeUtc(n));
      Console.WriteLine("Last access time: {0}", Directory.GetLastAccessTime(n));
      Console.WriteLine("UTC last access time: {0}", Directory.GetLastAccessTimeUtc(n));
        //Set the last write time to a different value.
      Directory.SetLastWriteTimeUtc(n, dtime2);
      Console.WriteLine("Changed last write time: {0}", Directory.GetLastWriteTimeUtc(n));
    }
  }
}
// Obviously, since this sample deals with dates and times, the output will vary
// depending on when you run the executable. Here is one example of the output:
//Creation Date: 1/3/2002 12:00:00 AM
//UTC creation Date: 1/3/2002 8:00:00 AM
//Last write time: 12/31/1998 4:00:00 PM
//UTC last write time: 1/1/1999 12:00:00 AM
//Last access time: 1/2/2002 4:00:00 PM
//UTC last access time: 1/3/2002 12:00:00 AM
//Changed last write time: 1/1/1999 12:00:00 AM
本文转自luojinghappy 51CTO博客,原文链接:http://blog.51cto.com/luojinghappy/142845,如需转载请自行联系原作者
相关文章
|
4月前
|
存储 监控 算法
基于 C# 的局域网计算机监控系统文件变更实时监测算法设计与实现研究
本文介绍了一种基于C#语言的局域网文件变更监控算法,通过事件驱动与批处理机制结合,实现高效、低负载的文件系统实时监控。核心内容涵盖监控机制选择(如事件触发机制)、数据结构设计(如监控文件列表、事件队列)及批处理优化策略。文章详细解析了C#实现的核心代码,并提出性能优化与可靠性保障措施,包括批量处理、事件过滤和异步处理等技术。最后,探讨了该算法在企业数据安全监控、文件同步备份等场景的应用潜力,以及未来向智能化扩展的方向,如文件内容分析、智能告警机制和分布式监控架构。
116 3
|
7月前
|
C#
c# 创建文件夹
在 C# 中,创建文件夹和文件依赖于 .NET 框架提供的 `System.IO` 命名空间中的类与操作系统交互。
|
11月前
|
C# Windows
C#实现指南:将文件夹与exe合并为一个exe
C#实现指南:将文件夹与exe合并为一个exe
658 9
|
11月前
|
安全 C# 数据安全/隐私保护
实现C#编程文件夹加锁保护
【10月更文挑战第16天】本文介绍了两种用 C# 实现文件夹保护的方法:一是通过设置文件系统权限,阻止普通用户访问;二是使用加密技术,对文件夹中的文件进行加密,防止未授权访问。提供了示例代码和使用方法,适用于不同安全需求的场景。
518 0
|
监控 安全 C#
使用C#如何监控选定文件夹中文件的变动情况?
使用C#如何监控选定文件夹中文件的变动情况?
259 19
|
C#
C# Winform 选择文件夹和选择文件
C# Winform 选择文件夹和选择文件
162 0
|
存储 C#
【Untiy 3D】C#中文件夹与读写文件操作详解(附测试代码 超详细)
【Untiy 3D】C#中文件夹与读写文件操作详解(附测试代码 超详细)
120 0
|
C#
C# 统计指定文件夹下的文件
C# 统计指定文件夹下的文件 需要:统计某一指定文件夹下的文件数目和文件的全路径 示例代码: /// <summary> /// 统计文件夹内的文件 /// </summary> /// <param name="path">指定的文件夹路径</param> /// <returns></returns> ...
101 0
|
开发框架 程序员 C#
【C#】winform实现锁定和解锁文件夹功能
有个朋友找我,问我有没有一个简单的小工具,能够直接对文件夹进行加密或者加锁,带着这个朋友的需求就简单的实现了加解锁文件夹功能的小工具!大家也可以想一想,在什么场景下需要对文件夹进行加锁操作呢? 1)一些比较敏感工作文件 2)防止别人复制个人的学习成果文件 等等场景
1125 0