不能访问我的文件夹或目录
本文关键字:文件夹 访问 我的 不能 | 更新日期: 2023-09-27 18:02:10
这个程序应该显示目录的路径,如果目录存在,那么它也应该显示里面的文件与以下扩展名(即。doc, .pdf, .jpg, .jpeg)。我在bin目录下创建了一个名为Max的文件夹,代码正在运行,但它告诉我目录不存在这是我在
下面的代码namespace sharp_lab_2
{
interface IFileOperation
{
//should handle the file
bool Accept(string fileName);
//handler function file
void Process(string fileName);
}
class FileProcByExt : IFileOperation
{
string extName;
string folderName;
public FileProcByExt(string ext = "")
{
extName = ext;
if (extName == "")
folderName = "MAXee";
else
folderName = extName.ToUpper();
}
public bool Accept(string fileName)
{
bool res = false;
if (extName == "" ) res = true; // all
if (Path.GetExtension(fileName) == "." + extName) res = true; // has extension
return res;
}
public void Process(string fileName)
{
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(fileName),
folderName));
File.Move(fileName,
Path.Combine(Path.GetDirectoryName(fileName),
folderName,
Path.GetFileName(fileName)));
}
}
class FileProcNameAfter10 : IFileOperation
{
static int cnt;
public bool Accept(string fileName)
{
return Path.GetFileNameWithoutExtension(fileName).Length > 10;
}
public void Process(string fileName)
{
File.Copy(fileName,
Path.Combine(Path.GetDirectoryName(fileName),
"longname_" + cnt + Path.GetExtension(fileName)));
cnt++;
}
}
class FileProcEnc: IFileOperation
{
public bool Accept(string fileName)
{
FileInfo fi = new FileInfo(fileName);
return fi.Length < 1024;
}
public void Process(string fileName)
{
File.Encrypt(fileName);
}
}
class Program
{
static void Main(string[] args)
{
try
{
string directoryPath = args[0];
string[] filesList, filesListTmp;
IFileOperation[] opList = { new FileProcNameAfter10(),
new FileProcEnc(),
new FileProcByExt("jpeg"),
new FileProcByExt("jpg"),
new FileProcByExt("doc"),
new FileProcByExt("pdf"),
new FileProcByExt("djvu")
};
if (Directory.Exists(directoryPath))
{
filesList = Directory.GetFiles(directoryPath);
while (true)
{
Thread.Sleep(500);
filesListTmp = Directory.GetFiles(directoryPath);
foreach (var elem in Enumerable.Except<string>(filesListTmp, filesList))
{
Console.WriteLine(elem);
foreach (var op in opList)
{
if (op.Accept(elem)) op.Process(elem);
}
}
filesList = filesListTmp;
if (Console.KeyAvailable == true && Console.ReadKey(true).Key == ConsoleKey.Escape) break;
}
}
else
{
Console.WriteLine("There is no such directory.");
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine("Error." + ex.Message);
}
}
}
}
您应该让您的代码所需的权限。如果对该目录没有最低的只读权限,Exists方法将返回false。如果在尝试确定指定文件是否存在时发生任何错误,则Exists方法返回false。这可能发生在引发异常的情况下,例如传递带有无效字符或过多字符的文件名,磁盘失败或丢失,或者调用者没有读取文件的权限。
msdn