C# 我正试图在我的usb闪存中取消隐藏一个空白的命名文件夹
对我来说,你的代码运行得很好,所以你能告诉我异常出现在哪一行吗。我猜是C# 我正试图在我的usb闪存中取消隐藏一个空白的命名文件夹,c#,C#,对我来说,你的代码运行得很好,所以你能告诉我异常出现在哪一行吗。我猜是File.SetAttributes(path,att)。然后检查正在处理的文件/目录。同样,我猜它类似于一个系统文件,其中不允许使用属性 关于File.SetAttributes(路径,att)的文档表示: ArgumentException:路径为空,仅包含空格,包含 无效字符,或文件属性无效 所以这应该是最后一部分 您可以这样更改代码以打印错误消息并继续处理其他文件: using System; using System
File.SetAttributes(path,att)代码>。然后检查正在处理的文件/目录。同样,我猜它类似于一个系统文件,其中不允许使用属性
关于File.SetAttributes(路径,att)的文档代码>表示:
ArgumentException:路径为空,仅包含空格,包含
无效字符,或文件属性无效
所以这应该是最后一部分
您可以这样更改代码以打印错误消息并继续处理其他文件:
using System;
using System.IO;
using System.Net;
namespace Diamond
{
class Program
{
static void Main(string[] args)
{
string filepath = @"j:";
getfiles(filepath);
Console.ReadKey();
}
public static void getfiles(string filePath)
{
string[] files = Directory.GetDirectories(filePath, "*.*", SearchOption.AllDirectories);
foreach (var file in files)
{
fileAttributes(file);
}
}
public static void fileAttributes(string path)
{
FileAttributes att = File.GetAttributes(path);
att = RemoveAtt(att, FileAttributes.Hidden | FileAttributes.ReadOnly);
File.SetAttributes(path, att);
Console.WriteLine(path);
}
private static FileAttributes RemoveAtt(FileAttributes att, FileAttributes attributesToRemove)
{
return att & ~attributesToRemove;
}
}
}
这样
public static void fileAttributes(string path)
{
try
{
FileAttributes att = File.GetAttributes(path);
att = RemoveAtt(att, FileAttributes.Hidden | FileAttributes.ReadOnly);
File.SetAttributes(path, att);
Console.WriteLine(path);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
问题是什么?这是我运行程序时弹出的问题=>`在mscorlib.dll中发生“System.ArgumentException”类型的未处理异常附加信息:无效的文件或目录属性值'。
var file = new FileInfo(filePath);
if (!file.Exists)
{
file.Create();
// Necessary line!
// Create it again to get valid file attributes
file = new FileInfo(filePath);
}
file.Attributes |= FileAttributes.Hidden;