problems creating lots of directories

C

Charlie Bear

i'm really stuck with this one can anyone help!

i have a website that uses c#. it creates a series of directories and
files from an xml source. when the xml changes, the directory that the
change applies to is deleted and recreated. All the children for that
directory are also re created (as the parent directory then has a
different name).

the problem i have is that *sometimes* the files or directories don't
get created. It either gets stuck creating the file or the directory.
i get either an io exception or a security exception. the .net runtime
has full access to the file system so it isn't a general permissions
thing. the securiy exception occurs on the last file or direcotory
created.
the last file or directory it creates before the exception is throw is
normally inaccessible and will be locked. i won't be able to delete it
manually in explorer - it will say i don't have permission even if i
am the administrator. i won't even be able to view the security info
or anything about it. After a while the file or folder will disappear
or become deletable.

No one else is using these files since it is on my test machine so it
can't be because of that.

the code i am using to create the direcotory and file is as follows.
this is run in a for loop - each xml node has directory name which
forms the path:

System.IO.DirectoryInfo di = new
DirectoryInfo(path);
di.Create();
di.Attributes = FileAttributes.Normal;
if (di.Exists)
{
System.Threading.Thread.Sleep(1000);
System.IO.FileInfo fi = new FileInfo(path + "\
\default.aspx");
if (!fi.Exists)
{
//Create a file to write to.
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine("Page Not Found.");
}
fi.Attributes = FileAttributes.Normal;
}

}

anyone got any ideas?
 
M

Michael Nemtsev

Hello Charlie,

How much directories do u create? coz afaik framework has a specific limit
(don't remember the exact number) of the directories

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

CB> has full access to the file system so it isn't a general permissions
CB> thing. the securiy exception occurs on the last file or direcotory
CB> created.
CB> the last file or directory it creates before the exception is throw
CB> is
CB> normally inaccessible and will be locked. i won't be able to delete
CB> it
CB> manually in explorer - it will say i don't have permission even if i
CB> am the administrator. i won't even be able to view the security info
CB> or anything about it. After a while the file or folder will
CB> disappear
CB> or become deletable.
 
G

Guest

Are you asking why you might not have access or what do do about it?

You may get access denied if the directory a sub-directory or file within
that hierarchy is in use ("current directory" setting constitutes a directory
in use). You'll have to handle these exceptions properly, there's nothing
you can do if another program is using these files/directories (like a defrag
program or a virus checker).

As to what to do about it; that's really up to your application. You must
handle these exceptions if you don't want to take down the current session.
 
C

Charlie Bear

i'm asking what to do about it!

it can happen when i'm creating one folder with a file or lots.

So are you saying that a directory could be locked by a virus scan
which in turn prevents the system creating a file / directory in it?

surely i should always be able to open a directory in explorer - which
is what i can't do after it's gone wrong. it doens't even have any
permissions or an owner. it's like it's half created

i handle the exception at the moment but i can't do anything with the
direcotry that has gone wrong for several mins afterwards which means
the application can't just try again.

is there something i can do to prevent this occuring. like slowing
down the creation of the files by adding a long thread.sleep or is
there a less intensive way of creating files?
 
S

shashank kadge

i'm asking what to do about it!

it can happen when i'm creating one folder with a file or lots.

So are you saying that a directory could be locked by a virus scan
which in turn prevents the system creating a file / directory in it?

surely i should always be able to open a directory in explorer - which
is what i can't do after it's gone wrong. it doens't even have any
permissions or an owner. it's like it's half created

i handle the exception at the moment but i can't do anything with the
direcotry that has gone wrong for several mins afterwards which means
the application can't just try again.

is there something i can do to prevent this occuring. like slowing
down the creation of the files by adding a long thread.sleep or is
there a less intensive way of creating files?


Try

sw.Flush();
sw.Close();

just before the 'using' loop ends.

-
shashank kadge
 
C

Charlie Bear

turning off my servers auto protect virus guard on the website
directory seems to have reduced the problem significantly. it still
occurs but when i retry it it works. i've got a try and catch around
the creation which will stop the process and alert the user to try
again.i've also added the "flush" and "close" just to be sure even
though its in a using block.

thanks for all your help guys...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top