// Create a new directory with specific permissions
Directory.CreateDirectory("myDirectory", UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
// Create a new file with specific permissions
FileStreamOptions options = new()
{
Access = FileAccess.Write,
Mode = FileMode.Create,
UnixCreateMode = UnixFileMode.UserRead | UnixFileMode.UserWrite,
};
using FileStream myFile = new FileStream("myFile", options);
// Get the mode of an existing file
UnixFileMode mode = File.GetUnixFileMode("myFile");
// Set the mode of an existing file
File.SetUnixFileMode("myFile", UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
.NET 7 Preview 7 发布,下一版本进入 RC 阶段
.NET 7 发布了最后一个预览版 Preview 7,在此之后将会进入 RC 阶段。
此版本主要变化包括对 System.LINQ、Unix 文件权限、底层结构、p/Invoke 源代码生成、代码生成和 websocket 的改进。
优化
System.LINQ
System.Linq
现在包含Order
和OrderDescending
方法,它们可以根据T
对IEnumerable
进行排序。IQueryable
现在也同样提供对此的支持。用法
此前需要通过引用自身的值来调用
OrderBy
/OrderByDescending
现在支持直接写成:
支持 Unix 文件模式
此前 .NET 没有内置支持获取和设置 Unix 文件权限,这些权限用于控制哪些用户可以读取、写入和执行文件以及目录。而且 P/Invoking 手动调用 syscalls 并不容易,因为有些 syscalls 在不同的发行版上有不同的公开方式。例如,在 Ubuntu 上,你可能要对
__xstat
进行 Pinvoke,在 Red Hat 上对stat
进行 Pinvoke,诸如此类。为此,Preview 7 引入了一个新的枚举:用法
优化底层
struct
:支持ref
字段.NET 7 运行时环境现在完全支持 ByRefLike 类型中的
ref
字段(即ref struct
)。此功能背后包含大量的语言设计,例如改进底层结构。借助此功能,以前需要在运行时环境中进行专门处理的类型(例如Span<T>
和ReadOnlySpan<T>
),现在可以在 C# 中完全实现。