Create a Blob Store in C#
by Christopher Morley
This is for when you want to have a lot of directories in a hierarchy in a short path. Over 18000 directories actually, in a 3 letter path not including slashes. Store the path plus the filenames in your database. Collision chance is very low upon insertion, especially with randomized numeric filenames. Probably only want to put files in the leaf nodes, but that's up to you. Makes for pretty fast access later to lots and lots of files.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CreateStore
{
class Program
{
static void Main(string[] args)
{
const int ASCII_A = 97;
const int LETTERS = 26; //in the alphabet
for (int l = ASCII_A; l < (ASCII_A + LETTERS); l++) Console.Write(l + ": " + (char)l);
Console.WriteLine();
Console.WriteLine("=================");
const string drive = "C:";
const string basepath = @"Stores";
const string storeName = "StoreB";
String path = drive + @"\" + basepath + @"\" + storeName;
bool skip = false;
if (!Directory.Exists(drive + @"\" + basepath))
{
Console.WriteLine("Base path does not exist. Please create it first.");
skip = true;
}
if (Directory.Exists(path))
{
Console.WriteLine("Store path already exists. Please delete it first.");
skip = true;
}
Console.WriteLine("=================");
int x = 0;
if (!skip)
{
Directory.CreateDirectory(path); x++;
for (int i = ASCII_A; i < (ASCII_A + LETTERS); i++)
{
string letter1 = "" + (char)i;
Directory.CreateDirectory(path + @"\" + letter1); x++;
for (int j = ASCII_A; j < (ASCII_A + LETTERS); j++)
{
string letter2 = "" + (char)j;
Directory.CreateDirectory(path + @"\" + letter1 + @"\" + letter2); x++;
for (int k = ASCII_A; k < (ASCII_A + LETTERS); k++)
{
string letter3 = "" + (char)k;
Directory.CreateDirectory(path + @"\" + letter1 + @"\" + letter2 + @"\" + letter3); x++;
}
}
Console.WriteLine("Number of Directories Created (so far): " + x);
}
Console.WriteLine("Number of Directories Created (total): " + x);
Console.WriteLine("Store Created.");
}
else
{
Console.WriteLine("Skipped store creation.");
}
Console.WriteLine("=================");
Random r = new Random();
for (int i = 0; i < 10; i++)
{
int ii = ASCII_A + r.Next(LETTERS);
int jj = ASCII_A + r.Next(LETTERS);
int kk = ASCII_A + r.Next(LETTERS);
Console.WriteLine("Ready to store something in : " + path + @"\" + (char)ii + @"\" + (char)jj + @"\" + (char)kk + @"\");
}
Console.WriteLine("=================");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
