Cимуляция cmd и attrib - C#
Формулировка задачи:
namespace cmd
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace cmd
{
public struct Command //структура для результата парсинга командной строки
{
public string command;
public string key;
public string parameter;
public string fileName;
public char rederict;
public string redirectFile;
public Command(string cmd, string k, string par, string fName, char redir, string redirFile)
{
command = cmd;
key = k;
parameter = par;
fileName = fName;
rederict = redir;
redirectFile = redirFile;
}
}
class Type
{
public void TypeFile(Command cl)
{
if (Path.GetExtension(cl.fileName) != ".txt")
{
Console.WriteLine("Файл не является текстовым");
return;
}
if (File.Exists(Environment.CurrentDirectory + '\\' + cl.fileName))
{
StreamReader objReader = new StreamReader(Environment.CurrentDirectory + "" + cl.fileName);
string sLine = "";
ArrayList arrText = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();
Console.WriteLine("\n Содержимое файла: " + cl.fileName);
foreach (string sOutput in arrText)
Console.WriteLine(sOutput);
Console.ReadLine();
}
else
Console.WriteLine("Ошибка открытия файла");
}
}
class Attrib
{
public void attribFile(Command cl)
{
string path = Environment.CurrentDirectory + "" + cl.fileName;
if (File.Exists(path))
{
FileAttributes attributes = File.GetAttributes(path);
if (cl.parameter != null)
{
switch (cl.parameter)
{
case "+r":
File.SetAttributes(path, FileAttributes.ReadOnly);
break;
case "-r":
attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
File.SetAttributes(path, attributes);
break;
}
}
else
{
string[] parseAtr = attributes.ToString().Split(new Char[] { ' ', '|' });
for (int i = 0; i < parseAtr.Length; i++)
{
parseAtr[i] = parseAtr[i].Substring(0, 1);
}
Console.WriteLine();
for (int i = 0; i < parseAtr.Length; i++)
{
Console.Write(parseAtr[i] + " ");
}
Console.Write(cl.fileName);
}
}
else if (cl.fileName == null)
{
Dir d = new Dir();
Parser p = new Parser();
IEnumerable<FileInfo> FileList = d.GetFiles(path);
foreach (var name in FileList)
{
if (name.Attributes != FileAttributes.Directory)
{
string[] parseAttrib = name.Attributes.ToString().Split(new Char[] { ' ', '|' });
for (int i = 0; i < parseAttrib.Length; i++)
{
parseAttrib[i] = parseAttrib[i].Substring(0, 1);
}
Console.WriteLine();
for (int i = 0; i < parseAttrib.Length; i++)
{
Console.Write(parseAttrib[i] + " ");
}
Console.Write(name.Name);
}
}
}
else
{
Console.WriteLine("Ошибка открытия файла.");
}
}
private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}
}
было решено. в куске кода не хватало проверки одной
Решение задачи: «Cимуляция cmd и attrib»
textual
Листинг программы
File.SetAttributes(path, attributes | FileAttributes.ReadOnly);