このプログラムは、ディレクトリのパスとディレクトリが存在する場合はそのディレクトリを表示し、次の拡張子 (.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);
}
}
}
}