32 lines
1.2 KiB
C#
32 lines
1.2 KiB
C#
using System.ComponentModel;
|
|
using System.Text.Json;
|
|
using TouchSocket.Dmtp.Rpc;
|
|
using TouchSocket.Rpc;
|
|
|
|
namespace Net;
|
|
|
|
public class GetFilesRpcServer : RpcServer
|
|
{
|
|
[Description("获取文件列表")] //服务描述,在生成代理时,会变成注释。
|
|
// [DmtpRpc("GetFiles")] //服务注册的函数键,此处为显式指定。默认不传参的时候,为该函数类全名+方法名的全小写。
|
|
public byte[]? GetFiles()
|
|
{
|
|
var rootPath = "E:\\zRoot\\S";
|
|
var directoryInfo = new DirectoryInfo(rootPath);
|
|
if (!directoryInfo.Exists)
|
|
return null;
|
|
var fileInfos = directoryInfo.GetFiles("*", SearchOption.TopDirectoryOnly);
|
|
var results = new ServerFileInfo[fileInfos.Length];
|
|
for (var index = 0; index < fileInfos.Length; index++)
|
|
{
|
|
var fileInfo = fileInfos[index];
|
|
results[index] = new ServerFileInfo(fileInfo.FullName, fileInfo.Length, fileInfo.LastWriteTime);
|
|
}
|
|
|
|
var serializeToUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(results);
|
|
return serializeToUtf8Bytes;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public record ServerFileInfo(string path, long siz, DateTime modifyTime); |