i have a folder that contains multiple folders , and these folders contain
multiple sub-folders , till we reach the last level in these subfolders that are html files .
i want to read this structure into sql related tables , and bind this structure into treeview , so that when i click on a parent node( a folder name) the child nodes expanded should be the sub-folders within the folder , till we reach the last node(s) that should be the html files, when i click on a node i should open the html file.
i want to create an explorer like interface in asp.net page .
please help .
How to read directory structure into SQL related tables
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Data.SqlTypes;
namespace CCMM
{
class DirectoryTreeLoader : IDisposable
{
const string connectionString = "Server=.;Database=WAM;Trusted_Connection=True;";
private SqlConnection Connection;
private SqlCommand Command;
private SqlParameter ParentDirectoryId;
private SqlParameter DirectoryName;
public DirectoryTreeLoader()
{
Connection = new SqlConnection(connectionString);
Command = Connection.CreateCommand();
ParentDirectoryId = new SqlParameter("@parent_id", SqlDbType.Int, 4);
DirectoryName = new SqlParameter("@name", SqlDbType.VarChar, 256);
ParentDirectoryId.IsNullable = true;
DirectoryName.IsNullable = false;
Command.Parameters.Add(ParentDirectoryId);
Command.Parameters.Add(DirectoryName);
Command.CommandType = CommandType.Text;
Command.CommandText = @"
insert dbo.directory ( parent_id , name ) values ( @parent_id , @name ) ;
select id = scope_identity() ;
".Trim();
return;
}
public void Load(DirectoryInfo root)
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
Command.Prepare();
}
Visit(null, root);
return;
}
private void Visit(int? parentId, DirectoryInfo dir)
{
ParentDirectoryId.SqlValue = parentId.HasValue ? new SqlInt32(parentId.Value) : SqlInt32.Null;
DirectoryName.SqlValue = new SqlString(dir.Name);
object o = Command.ExecuteScalar();
int id = (int)(decimal)o;
foreach (DirectoryInfo subdir in dir.EnumerateDirectories())
{
Visit(id, subdir);
}
return;
}
public void Dispose()
{
if (Command != null)
{
Command.Cancel();
Command.Dispose();
Command = null;
}
if (Connection != null)
{
Connection.Dispose();
Connection = null;
}
return;
}
}
}
code of the button on the form calling the class :
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo root = new DirectoryInfo(@"D:\website\AIO_GO");
using (DirectoryTreeLoader loader = new DirectoryTreeLoader())
{
loader.Load(root);
}
return;
}
Talbe structure
CREATE TABLE [dbo].[directory](
[id] [int] IDENTITY(1,1) NOT NULL,
[parent_id] [int] NULL,
[name] [nvarchar](256) NOT NULL,
[filename] [nvarchar](250) NOT NULL,
CONSTRAINT [PK__director__3213E83F70BE5160] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UQ__director__853DFACFA2660FAC] UNIQUE NONCLUSTERED
(
[id] ASC,
[name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[directory] WITH CHECK ADD CONSTRAINT [FK__directory__paren__286302EC] FOREIGN KEY([parent_id])
REFERENCES [dbo].[directory] ([id])
GO
ALTER TABLE [dbo].[directory] CHECK CONSTRAINT [FK__directory__paren__286302EC]
GO