Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Access Denied (and for once not a simple permission problem)

Status
Not open for further replies.

digiduck

Programmer
Apr 4, 2003
95
US
I have some code that does the following:

1. look for directory - if it exists, delete it
2. create the directory
3. create a new textfile in the directory

Now the code works perfect - it does just what it should except it only works for maybe 2 runs and then it roadblocks with an Access Denied error message on the directory. I can't even get into the directory as an administrator user on the system anymore, it's completely locked. Nothing in my code or the filesystem changes between runs so what could be the problem? And how in the world can i get rid of those locked directories?

Code:
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
Directory.CreateDirectory(path);

StreamWriter writer = null;
try
{
writer = File.CreateText(Path.Combine(path, "myfile.txt"));
writer.Wrtie("blah");
writer.Close();
}
catch
{
if (writer != null) writer.close
}

ud


tkc
 
If you are not able to catch the error using try-catch then :
- call Flush() before Close(); but Close() should do the job
-use a FileIOPermission object and to force a SecurityException at run time to see if there are callers higher in the call stack that have no permission specified by the current instance.
Example:
Code:
FileIOPermission perm = new FileIOPermission(FileIOPermissionAccess.Write, path);
perm.Demand();
Stream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
fileStream.Write();
fileStream.Close();
obislavu
 
To get rid of the directory, right click on the directory and choose properties. Then go to security tab. Click on advanced and then on the owner tab. You should see the current owner as "unknown". Select "Administrators" from the list below and click on OK.
Go back to where you clicked advanced and add yourself with permissions, click ok and delete the folder.
 
[highlight]korach[/highlight] -- the folders in question do not have a security tab, the only tabs they have are General and Sharing. I check that before and it was one of the thigns that boggled me -- they should have the Security tab but don't.

[highlight]obislavu[/highlight] -- I'm trying this out now, I'll let you know how it goes.

ud


tkc
 
If you want a security tab make sure that:
1. You use NTFS and not FAT32. to check that right click on the drive and choose properties. In the opened window you'll see that on top.
2. You don't use simple file sharing. to check that open windows explorer. On tools menu choose "folder options". Go to "view" tab and you'll see a lot of checkboxes. There is one called "Use simple file sharing (recommended)". Uncheck this. The default for XP is checked.

Maybe this will fix your bug also...
 
Sorry korach -- that isn't the issue. Other folders have the Security tab at the same time that these folder don't have it. Even the parent folder of the bad directories has a Security tab.

I did restart the computer the it created the Security tab -- but my real problem is why it is creating the folders without the tab. :\ -- I think I have found a workaround in my code to the problem so hopefully it won't come up again.

ud


tkc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top