当您右键单击文件上的\ Properties 时,我正在尝试设置导致只读复选框的标志。

谢谢!

有帮助吗?

解决方案

两种方式:

System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
fileInfo.IsReadOnly = true/false;

// Careful! This will clear other file flags e.g. FileAttributes.Hidden
File.SetAttributes(filePath, FileAttributes.ReadOnly/FileAttributes.Normal);

FileInfo上的IsReadOnly属性实际上是在第二种方法中手动完成的位翻转。

其他提示

设置只读标志,实际上使文件不可写:

File.SetAttributes(filePath,
    File.GetAttributes(filePath) | FileAttributes.ReadOnly);

删除只读标志,实际上使文件可写:

File.SetAttributes(filePath,
    File.GetAttributes(filePath) & ~FileAttributes.ReadOnly);

切换只读标志,使其与现在的情况相反:

File.SetAttributes(filePath,
    File.GetAttributes(filePath) ^ FileAttributes.ReadOnly);

这基本上是位掩码。您设置一个特定位来设置只读标志,清除它以删除标志。

请注意,上述代码不会更改文件的任何其他属性。换句话说,如果在执行上面的代码之前隐藏了文件,那么它也会在之后保持隐藏状态。如果只是将文件属性设置为 .Normal .ReadOnly ,那么最终可能会丢失其他标志。

c#:

File.SetAttributes(filePath,FileAttributes.Normal);

File.SetAttributes(filePath,FileAttributes.ReadOnly);

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top