Hi,
I want list file directory in treeview control with checkboxes .Tnen perform delete function to delete selectef file or folder in c sharp.pls reply
Thanks n Regards,
Ankita
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
shital kothariPosted Jun 11, 2012, 7:20 PM
shital kothariPosted Jun 11, 2012, 7:16 PM
Hi ankita!
You may use following code to list files and folders to treeview control...
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 nListingFoldersToTreeView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
LoadFoldersInTreeView(treeView1);
}
void LoadFoldersInTreeView(TreeView treeName)
{
treeName.BeginUpdate();
treeName.Nodes.Add("Desktop");
treeName.Nodes[0].Nodes.Add("My Documents");
treeName.Nodes[0].Nodes.Add("My Computer");
treeName.Nodes[0].Nodes.Add("My Network places");
DirectoryInfo dirInfo = new DirectoryInfo(@"C:\");
TreeNode node = new TreeNode();
GetFiles(dirInfo, node);
treeName.EndUpdate();
}
void GetFiles(DirectoryInfo d, TreeNode node)
{
DirectoryInfo[] dInfo = d.GetDirectories();
if (dInfo.Length > 0)
{
TreeNode treeNode = new TreeNode();
foreach (DirectoryInfo driSub in dInfo)
{
treeNode = node.Nodes.Add(driSub.Name);
// once working I will make it recursive
//GetFiles(driSub, treeNode);
}
}
}
}
}
You can use following snippet:
// Simple synchronous file copy operations with no user interface.
// To run this sample, first create the following directories and files:
// C:\Users\Public\TestFolder
// C:\Users\Public\TestFolder\test.txt
// C:\Users\Public\TestFolder\SubDir\test.txt
public class SimpleFileCopy
{
static void Main()
{
string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
// To copy a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
// To copy all the files in one directory to another directory.
// Get the files in the source folder. (To recursively iterate through
// all subfolders under the current directory, see
// "How to: Iterate Through a Directory Tree.")
// Note: Check for target path was performed previously
// in this code example.
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
Thanking you,