一些C#的小技术,代码部分不考虑执行效率,只为分享一些思路。
1.用 C# 轻松读取、改变文件的创建、修改、访问时间
[C#]代码
1 | FileInfo fi = new FileInfo( "C:\\test.txt" ); |
2 | Console.WriteLine(fi.CreationTime.ToString()); |
3 | Console.WriteLine(fi.LastWriteTime.ToString()); |
4 | Console.WriteLine(fi.LastAccessTime.ToString()); |
7 | File.SetCreationTime( "C:\\test.txt" , DateTime.Now.AddYears(-1)); |
8 | File.SetLastWriteTime( "C:\\test.txt" , DateTime.Now.AddYears(-2)); |
9 | File.SetLastAccessTime( "C:\\test.txt" , DateTime.Now.AddYears(-3)); |
2.将Excel列号转化为字母字符
比如
数值0,对应A列,
数值1,对应B列,
...
依次类推
...
数值26,对应AA列,
数值27,对应AB列
......
[C#]代码
01 | for ( int i = 0; i < 10000; i++) |
03 | List< char > chs = new List< char >(); |
07 | chs.Add(( char )(m % 26 + 65)); |
10 | Console.WriteLine( string .Join( "" , chs.Reverse< char >())); |
3.用C#创建一个FTP服务器
[C#]代码
02 | using System.Collections.Generic; |
06 | using System.Net.Sockets; |
08 | using System.Threading; |
10 | namespace SharpFtpServer |
12 | public class FtpServer |
14 | private TcpListener _listener; |
22 | _listener = new TcpListener(IPAddress.Any, 21); |
26 | _listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _listener); |
31 | if (_listener != null ) |
37 | private void HandleAcceptTcpClient(IAsyncResult result) |
39 | TcpClient client = _listener.EndAcceptTcpClient(result); |
40 | _listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _listener); |
持续更新中……