By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,659 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,659 IT Pros & Developers. It's quick & easy.

problems creating lots of directories

P: n/a
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?

Apr 27 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

CBhas full access to the file system so it isn't a general permissions
CBthing. the securiy exception occurs on the last file or direcotory
CBcreated.
CBthe last file or directory it creates before the exception is throw
CBis
CBnormally inaccessible and will be locked. i won't be able to delete
CBit
CBmanually in explorer - it will say i don't have permission even if i
CBam the administrator. i won't even be able to view the security info
CBor anything about it. After a while the file or folder will
CBdisappear
CBor become deletable.
Apr 27 '07 #2

P: n/a
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.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Charlie Bear" wrote:
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?

Apr 27 '07 #3

P: n/a

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?
Apr 27 '07 #4

P: n/a
hi,
Use

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

inside the using loop.
-
shashank kadge.

On Apr 27, 9:36 am, Charlie Bear <char...@contrapositive.tvwrote:
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?

Apr 27 '07 #5

P: n/a
On Apr 27, 10:59 am, Charlie Bear <char...@contrapositive.tvwrote:
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

Apr 27 '07 #6

P: n/a
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...


Apr 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.