Careful
Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
namespace Pr2
{
class Program
{
public delegate void threadDelegate(string message);
static public event threadDelegate unEvent;
static void Main(string[] args)
{
bool dirValid = false;
string path = “”;
string responsePrompt=”";
while (!dirValid && path != “q”)
{
Console.WriteLine(“\nIntroduceti calea spre director (sau q pentru a iesi din bucla): “);
path = Console.ReadLine();
dirValid = dirExists(path);
if (dirValid && dirIsEmpty(path))
{
Console.WriteLine(“\nDirectorul a fost gasit dar este gol, generez fisiere.”);
generator_de_fisiere(path);
}
}
if (dirValid)
Console.WriteLine(“\nDirectorul ” + path + ” a fost gasit.”);
else
{
//s-a introdus q
return;
}
responsePrompt = “”;
while (responsePrompt != “n” && responsePrompt != “N” && responsePrompt != “y” && responsePrompt != “Y”)
{
Console.WriteLine(“\nDoriti sa stergeti continutul fiserului ” + path + ” ? (y/n)”);
responsePrompt = Console.ReadLine();
}
if (responsePrompt == “Y” || responsePrompt == “y”)
{
//stergere
stergere(path);
}
Console.WriteLine(“\t\tApasati orice tasta pentru a termina executia programului.”);
Console.ReadKey();
}
static bool dirExists(string path)
{
return Directory.Exists(path);
}
static bool dirIsEmpty(string path)
{
string[] fisiere = Directory.GetFiles(path);
string[] directoare = Directory.GetDirectories(path);
/*foreach (string s in fisiere)
{
Console.WriteLine(s);
}
foreach (string s in directoare)
{
Console.WriteLine(s);
}*/
if (fisiere.Length > 0 || directoare.Length > 0)
return false;
else
return true;
}
static void stergere(string path)
{
if (!dirExists(path))
{
Console.WriteLine(“Directorul nu exista.”);
return;
}
if (dirIsEmpty(path))
{
Console.WriteLine(“Directorul “+path+” este gol.”);
return;
}
//
string[] fisiere = Directory.GetFiles(path);
//stergere fisere
Thread[] threads = new Thread[fisiere.Length];
//event
unEvent += new threadDelegate(afisare_mesaj);
for (int i = 0; i < fisiere.Length; i++)
{
threads[i] = new Thread(sterge_fisier);
threads[i].Start(fisiere[i]);
//Thread.Sleep(500);
}
//reapel pt stergere directoare
string[] directoare = Directory.GetDirectories(path);
foreach (string dir in directoare)
{
stergere(dir);
Thread thDir = new Thread(stergere_director);
thDir.Start(dir);
}
}
static void sterge_fisier(object path)
{
//stergerea logica
stergere_logica(path as string);
unEvent.Invoke(“a fost sters logic “+path);
//stergerea fizica
stergere_fizica(path as string);
}
static void stergere_logica(string path)
{
byte[] bytes = File.ReadAllBytes(path);
for (int i = 0; i < bytes.Length; i++)
bytes[i] = 0;
File.WriteAllBytes(path, bytes);
}
static void stergere_fizica(string path)
{
File.Delete(path);
}
static void afisare_mesaj(string message)
{
Console.WriteLine(“[{0}] – {1}.”,Thread.CurrentThread.ManagedThreadId,message);
}
static void generator_de_fisiere(string path)
{
for (int i=0; i<600; i++)
{
StringBuilder sb = new StringBuilder();
sb.Append(path);
sb.Append(“/”);
sb.Append(i);
sb.Append(“fis.txt”);
//
FileStream fs = File.Open(sb.ToString(), FileMode.OpenOrCreate, FileAccess.ReadWrite);
byte[] bytes = new byte[300];
for (int j = 0; j < 300; j++)
bytes[j] = (byte) j;
fs.Write(bytes, 0, 300);
}
}
static void stergere_director(object path)
{
while (Directory.GetFiles(path as string).Length != 0)
{
Thread.Sleep(100);
}
Directory.Delete(path as string);
}
}
}
Description
500 si 1000
Filed under: Samples