1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Explorateur
{
public partial class WindowsFormsApplication1 : Form
{
private string path = "E:/" ;
public WindowsFormsApplication1()
{
InitializeComponent();
this.tv.ContextMenuStrip = this.contextMenuStrip1;
// EVENTS du TV
this.tv.MouseDown+=new MouseEventHandler(tv_MouseDown);
this.tv.AfterLabelEdit+=new NodeLabelEditEventHandler(tv_AfterLabelEdit);
}
//private void WindowsFormsApplication1_Load(object sender, EventArgs e)
//{
// this.tv.ContextMenuStrip = this.contextMenuStrip1;
// // Clear All Nodes if Already Exists
// this.tv.Nodes.Clear();
// if (path != "" && Directory.Exists(path))
// LoadDirectory(path);
//}
private void btnSelectFolder_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
path = dlg.SelectedPath;
txtDirectory.Text = path ;
this.tv.Nodes.Add(new TreeNode("Explorateur"));
LoadDirectory(path);
}
}
private void LoadDirectory(string dir)
{
DirectoryInfo di = new DirectoryInfo(dir);
TreeNode newNode = this.tv.Nodes.Add(di.Name);
newNode.Tag = di.FullName;
newNode.StateImageIndex = 0;
LoadFiles(dir, newNode);
LoadSubDirectories(dir, newNode);
}
private void LoadSubDirectories(string dir, TreeNode node)
{
string[] subdirectoryEntries = Directory.GetDirectories(dir);
// Loop through them to see if they have any other subdirectories
foreach (string subdirectory in subdirectoryEntries)
{
DirectoryInfo di = new DirectoryInfo(subdirectory);
TreeNode newNnode = node.Nodes.Add(di.Name);
newNnode.StateImageIndex = 0;
newNnode.Tag = di.FullName;
LoadFiles(subdirectory, newNnode);
LoadSubDirectories(subdirectory, newNnode);
}
}
// MODIF POUR AFFICHER LES FICHIERS *.cpc
private void LoadFiles(string dir, TreeNode td)
{
string[] files = Directory.GetFiles(dir, "*.txt");
// Loop through them to see files
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
TreeNode tnode = td.Nodes.Add(fi.Name);
tnode.Tag = fi.FullName;
tnode.StateImageIndex = 1;
}
// AJOUT
files = Directory.GetFiles(dir, "*.cpc");
// Loop through them to see files
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
TreeNode tnode = td.Nodes.Add(fi.Name);
tnode.Tag = fi.FullName;
tnode.StateImageIndex = 2;
}
}
private void NewFolderItemToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.tv.SelectedNode == null) return;
TreeNode selNode = this.tv.SelectedNode;
string pathDir = (string)selNode.Tag;
DirectoryInfo di = new DirectoryInfo(pathDir);
// add subdirectory to di
string newDirectoryName = GetNewFolderName(di);
// Adds new node as a child node of the currently selected node.
TreeNode newNode = new TreeNode(newDirectoryName);
newNode.Tag = di.FullName + @"\\" + newDirectoryName;
newNode.ImageKey = "NewFolder";
selNode.Nodes.Add(newNode);
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.tv.SelectedNode == null) return;
TreeNode selNode = this.tv.SelectedNode;
string pathDir = (string)selNode.Tag;
if (pathDir.Contains("cpc") || pathDir.Contains("xml"))
{
FileInfo fi = new FileInfo(pathDir);
fi.Delete();
}
else
{
DirectoryInfo di = new DirectoryInfo(pathDir);
di.Delete(true);
}
// Removes currently selected node
this.tv.Nodes.Remove(selNode);
}
private string GetNewFileNameCPC(DirectoryInfo di)
{
string newItem = "NewItem";
string[] files = Directory.GetFiles(di.FullName, "*.cpc");
string fileName = di.FullName + @"\" + newItem;
int countFile = 0;
if (files.Length == 0)
countFile = 1;
else
countFile = files.Length + 1;
fileName += countFile.ToString() + ".cpc";
FileInfo fi = new FileInfo(fileName);
//Create a file .
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(newItem);
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
newItem += countFile.ToString() + ".cpc";
return newItem;
}
private string GetNewFileNameTXT(DirectoryInfo di)
{
string newItem = "NewItem";
string[] files = Directory.GetFiles(di.FullName, "*.txt");
string fileName = di.FullName + @"\" + newItem;
int countFile = 0;
if (files.Length == 0)
countFile = 1;
else
countFile = files.Length + 1;
fileName += countFile.ToString() + ".txt";
FileInfo fi = new FileInfo(fileName);
//Create a file .
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(newItem);
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
newItem += countFile.ToString() + ".txt";
return newItem;
}
private string GetNewFolderName(DirectoryInfo di)
{
string newFolder = "NewFolder";
string[] subdirs = Directory.GetDirectories(di.FullName, newFolder + "*");
string dirName = di.FullName + @"\\" + newFolder;
int countDir = 0;
if (subdirs.Length == 0)
countDir = 1;
else
countDir = subdirs.Length + 1;
dirName += countDir.ToString();
DirectoryInfo newDir = new DirectoryInfo(dirName);
//Create a directory
newDir.Create();
newFolder += countDir.ToString();
return newFolder;
}
// CODE A VIRER CAR IL FOUT LA PAGAILLE ET OCCULTE LE DECLENCHEMENT
// DE L'EVENT TREEVIEW_MOUSEDOWN UTILISE POUR RENOMMER L'ETIQUETTE
// DE NOEUD (et des objets afferents DirectoryInfo et FilInfo)
// L'EVENT openFileCPCToolStripMenuItem FAIT LE BOULOT
//private void tv_NodeMouseDoubleClic(object sender, MouseEventArgs e)
//{
// if (this.tv.SelectedNode == null) return;
// TreeNode selNode = this.tv.SelectedNode;
// if (selNode.Text.EndsWith("cpc") )
// {
// string pathDir = (string)selNode.Tag;
// FileInfo fi = new FileInfo(pathDir);
// // edit file
// using (StreamReader sr = new StreamReader(fi.FullName))
// {
// BoxEditor.Clear();
// BoxEditor.Text = sr.ReadToEnd();
// tabControl1.SelectedIndex = 1;
// tabPage2.Show();
// }
// }
// // A VIRER
// if (selNode.Text.EndsWith("CPC"))
// {
// string pathDir = (string)selNode.Tag;
// FileInfo fi = new FileInfo(pathDir);
// // edit file
// using (StreamReader sr = new StreamReader(fi.FullName))
// {
// BoxEditor.Clear();
// BoxEditor.Text = sr.ReadToEnd();
// tabControl1.SelectedIndex = 1;
// tabPage2.Show();
// }
// }
//}
private void openFileCPCToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.tv.SelectedNode == null) return;
TreeNode selNode = this.tv.SelectedNode;
if (selNode.Text.EndsWith("cpc") || selNode.Text.EndsWith("CPC"))
{
string pathDir = (string)selNode.Tag;
FileInfo fi = new FileInfo(pathDir);
// edit file
using (StreamReader sr = new StreamReader(fi.FullName))
{
BoxEditor.Clear();
BoxEditor.Text = sr.ReadToEnd();
tabControl1.SelectedIndex = 1;
tabPage2.Show();
}
}
}
private void openFileTextToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.tv.SelectedNode == null) return;
TreeNode selNode = this.tv.SelectedNode;
if (selNode.Text.EndsWith("txt") || selNode.Text.EndsWith("TEXT"))
{
string pathDir = (string)selNode.Tag;
FileInfo fi = new FileInfo(pathDir);
// edit file
using (StreamReader sr = new StreamReader(fi.FullName))
{
BoxEditor.Clear();
BoxEditor.Text = sr.ReadToEnd();
tabControl1.SelectedIndex = 1;
tabPage2.Show();
}
}
}
private void newFileCPCToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.tv.SelectedNode == null) return;
string pathDir = (string)this.tv.SelectedNode.Tag;
DirectoryInfo di = new DirectoryInfo(pathDir);
// add file to di
string newFileName = GetNewFileNameCPC(di);
// Adds new node as a child node of the currently selected node.
TreeNode newNode = new TreeNode(newFileName);
newNode.Tag = di.FullName + @"\\" + newFileName;
newNode.ImageKey = "NewFile";
this.tv.SelectedNode.Nodes.Add(newNode);
}
private void newFileTextToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.tv.SelectedNode == null) return;
string pathDir = (string)this.tv.SelectedNode.Tag;
DirectoryInfo di = new DirectoryInfo(pathDir);
// add file to di
string newFileName = GetNewFileNameTXT(di);
// Adds new node as a child node of the currently selected node.
TreeNode newNode = new TreeNode(newFileName);
newNode.Tag = di.FullName + @"\\" + newFileName;
newNode.ImageKey = "NewFile";
this.tv.SelectedNode.Nodes.Add(newNode);
}
// ADDENDUM CODE POUR RENOMMER UN DIRECTORYINF OU UN FILEINFO
// Extrait de la MSDN Library fr
/* Grab the tree node under the mouse pointer and
save it in the mySelectedNode variable. */
private TreeNode mySelectedNode = null;
private void tv_MouseDown(object sender, MouseEventArgs e)
{
mySelectedNode = tv.GetNodeAt(e.X, e.Y);
}
private void renameToolStripMenuItem_Click(object sender, EventArgs e)
{
if (mySelectedNode != null && mySelectedNode.Parent != null)
{
this.tv.SelectedNode = mySelectedNode;
this.tv.LabelEdit = true;
if (!mySelectedNode.IsEditing)
{
mySelectedNode.BeginEdit();
}
}
else
{
MessageBox.Show("No tree node selected or selected node is a root node.\n" +
"Editing of root nodes is not allowed.", "Invalid selection");
}
}
private void tv_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
if (e.Label != null)
{
if (e.Label.Length > 0)
{
if (e.Label.IndexOfAny(new char[] { '@', '\\', ',', '!' }) == -1)
{
// Stop editing without canceling the label change.
RenameItem(e.Node, e.Label);
e.Node.EndEdit(false);
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e.CancelEdit = true;
MessageBox.Show("Invalid tree node label.\n" +
"The invalid characters are: '@','.', ',', '!'",
"Node Label Edit");
e.Node.BeginEdit();
}
}
else
{
/* Cancel the label edit action, inform the user, and
place the node in edit mode again. */
e.CancelEdit = true;
MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
"Node Label Edit");
e.Node.BeginEdit();
}
this.tv.LabelEdit = false;
}
}
// MODIF A AJOUTER " || oldPathDir.Contains("cpc")"
private void RenameItem(TreeNode node, string newLabel)
{
string oldPathDir = (string)node.Tag;
if (oldPathDir.Contains("txt") ||
oldPathDir.Contains("xml")||
oldPathDir.Contains("cpc"))// it is a fileInfo
{
FileInfo srcFi = new FileInfo(oldPathDir);
string srcDircName = srcFi.FullName;
int index = srcDircName.LastIndexOf('\\');
string partDirName = srcDircName.Remove(index);
string dstDirName = partDirName + @"\\" + newLabel;
if (!srcDircName.Equals(dstDirName))
{
Directory.Move(srcDircName, dstDirName);// move source to destination
//CODE A VIRER
//srcFi.MoveTo(dstFileName);
//srcFi.Delete();
}
}
else // it is a directorinfo
{
DirectoryInfo srcDir = new DirectoryInfo(oldPathDir);
string srcDirName = srcDir.FullName;
int index = srcDirName.LastIndexOf('\\');
string partDirName = srcDirName.Remove(index);
string dstDirName = partDirName + @"\\" + newLabel;
if (!srcDirName.Equals(dstDirName))
{
Directory.Move(srcDirName, dstDirName);// move source to destination
//CODE A VIRER
//srcDir.MoveTo(dstDirName);
//srcDir.Delete(true);
}
}
}
}
} |