int count = 1; void DirSearch(string sDir) { bool bitir = false; try { foreach (string d in Directory.GetDirectories(sDir)) { if (bitir) { break; } foreach (string f in Directory.GetDirectories(d)) { if(bitir) { break; } if(f.Contains("Resim")) { listBox1.Items.Add(count +" "+ f); //string userName = System.Environment.MachineName +@"\Network Service"; //string userName = System.Environment.MachineName +@"\ASPNET"; string userName = @"ASPNETMVC"; DirectoryInfo dInfo1 = new DirectoryInfo(f); DirectorySecurity dSecurity1 = dInfo1.GetAccessControl(); dSecurity1.AddAccessRule(new FileSystemAccessRule(userName, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); dInfo1.SetAccessControl(dSecurity1); count = count+1; bitir = true; } } if (bitir) { break; } DirSearch(d); } } catch (System.Exception excpt) { MessageBox.Show(excpt.Message); } } private void button1_Click(object sender, EventArgs e) { DirSearch("C:\\vHosts"); }