这个程序应该显示一个目录的路径和目录如果它存在那么它还应该显示带有以下扩展名的文件(即.doc、.pdf、.jpg、.jpeg)我创建了一个名为 max 的文件夹代码正在运行的 bin 目录,但它告诉我该目录不存在这是我下面的代码
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);
}
}
}
}