File.SetAttributes(String,FileAttributes)是一个内置的File类方法,用于在指定路径上设置文件的指定文件属性。文件属性是授予或拒绝的某些特定权限。这些权限适用于访问文件的用户或操作系统。这些属性例如是只读,存档,系统,隐藏等。
句法:
public static void SetAttributes (string path, System.IO.FileAttributes fileAttributes);
参数:该函数接受两个参数,如下所示:
- path: This is the specified file path.
- fileAttributes: This is the bitwise combination of the enumeration values.
例外情况:
- ArgumentException:路径为空,仅包含空格,无效字符或文件属性无效。
- PathTooLongException:指定的路径,文件名或两者都超过系统定义的最大长度。
- NotSupportedException:路径格式无效。
- DirectoryNotFoundException:指定的路径无效。
- FileNotFoundException:找不到文件。
- UnauthorizedAccessException:该路径指定了一个只读文件。或当前平台不支持此操作。或路径指定目录。或呼叫者没有所需的权限。
下面是说明File.SetAttributes(String,FileAttributes)方法的程序。
程序1:在运行下面的代码之前,将创建一个文件file.txt ,其内容如下所示:
CSharp
// C# program to illustrate the usage
// of File.SetAttributes(String) method
// Using System, System.IO
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
class GFG {
public static void Main()
{
// Specifying a file
string path = @"file.txt";
// Getting the file attributes
FileAttributes attributes = File.GetAttributes(path);
// Checking if the file is having whether hidden attributes
// If the file is having hidden attribute then
// that attribute will be removed
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) {
// Removing the file's hidden attribute
attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
// Calling the SetAttributes() function
File.SetAttributes(path, attributes);
Console.WriteLine("The {0} file is no longer hidden.", path);
}
else {
// Calling the SetAttributes() function to
// set hidden attribute
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
Console.WriteLine("The {0} file is now hidden.", path);
}
}
private static FileAttributes RemoveAttribute(FileAttributes attributes,
FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}
}
CSharp
// C# program to illustrate the usage
// of File.SetAttributes(String) method
// Using System, System.IO
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
class GFG {
public static void Main()
{
// Specifying a file
string path = @"gfg.txt";
// Create the file if it does not exist.
if (!File.Exists(path)) {
File.Create(path);
}
// Getting the file attributes
FileAttributes attributes = File.GetAttributes(path);
// Checking if the file is having whether hidden attributes
// If the file is having hidden attribute then
// that attribute will be removed
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) {
// Removing the file's hidden attribute
attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
// Calling the SetAttributes() function
File.SetAttributes(path, attributes);
Console.WriteLine("The {0} file is no longer hidden.", path);
}
else {
// Calling the SetAttributes() function to
// set hidden attribute
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
Console.WriteLine("The {0} file is now hidden.", path);
}
}
private static FileAttributes RemoveAttribute(FileAttributes attributes,
FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}
}
输出:
The file.txt file is now hidden.
程序2:最初没有创建文件。在代码下方,其本身创建了一个文件gfg.txt 。
尖锐的
// C# program to illustrate the usage
// of File.SetAttributes(String) method
// Using System, System.IO
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
class GFG {
public static void Main()
{
// Specifying a file
string path = @"gfg.txt";
// Create the file if it does not exist.
if (!File.Exists(path)) {
File.Create(path);
}
// Getting the file attributes
FileAttributes attributes = File.GetAttributes(path);
// Checking if the file is having whether hidden attributes
// If the file is having hidden attribute then
// that attribute will be removed
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) {
// Removing the file's hidden attribute
attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
// Calling the SetAttributes() function
File.SetAttributes(path, attributes);
Console.WriteLine("The {0} file is no longer hidden.", path);
}
else {
// Calling the SetAttributes() function to
// set hidden attribute
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
Console.WriteLine("The {0} file is now hidden.", path);
}
}
private static FileAttributes RemoveAttribute(FileAttributes attributes,
FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}
}
输出:
The gfg.txt file is now hidden.