Here's a recursive version that only requires one create and one delete:
(Use with caution it hasn't been tested that thoroughly)
bool CanCreate(string path)
{
try
{
var toCreate = new DirectoryInfo(path);
if(toCreate.Exists) return true;
var toDelete = MostGeneralMissing(toCreate);
toCreate.Create();
toDelete.Delete(true);
return true;
} catch {
return false;
// or throw if you prefer
}
}
DirectoryInfo MostGeneralMissing(DirectoryInfo dir)
{
if(dir == null || dir.Parent == null) return null;
if(dir.Exists == false && dir.Parent.Exists) return dir;
return MostGeneralMissing(dir.Parent);
}
It still has all the drawbacks from before though (i.e. if you don't have
delete privileges then you get a spare directory hanging around).
On Thu, Jun 17, 2010 at 7:01 PM, silky <[email protected]> wrote:
> On Thu, Jun 17, 2010 at 8:53 PM, James Chapman-Smith
> <[email protected]> wrote:
> > Hi silky,
> >
> > It only creates then deletes folders that don't exist so nothing else
> (bar
> > some sort of file system watching rogue program) will write to those
> > folders.
> >
> > But what do you mean "terrible coding style"? The only thing that I can
> > fault is the use of the general-purpose `catch` statement.
> >
> > Please hit me with your criticisms. I want to learn. :-)
>
> I was kind of making a joke; no-one wants to hear me complain yet
> again about the appropriateness of "var". But I'm not a big fan of
> many changes in .NET, or Mr Scripting himself (he's probably not a bad
> guy, but I'm not a fan of many if any of the "dynamic" changes; but
> who knows, perhaps that attitude will change).
>
>
> > Cheers.
> >
> > James.
>
> --
> silky
>
> http://www.programmingbranch.com/
>
--
Michael M. Minutillo
Indiscriminate Information Sponge
Blog: http://wolfbyte-net.blogspot.com