public static void EnsureDirectoryExists(string targetPath)
{
string dir = Path.GetDirectoryName(targetPath);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
}