Can not create a new file with File.Create -- throws exception:Access Denied

T

thephatp

I'm trying to do something extremely basic, and I don't remember
having these problems in XP.

I'm running a service, logged in as an administrator. I created a
directory in windows explorer (as administrator), and set all
permissions/security for all users (including the Everyone group) to
full control.

The following code still does not work:

FileStream fs;
try
{

System.Windows.Forms.MessageBox.Show( this.sFileStoreLocation + "\\" +
sFilename + ".txt" );
fs = File.Create( this.sFileStoreLocation + "\\" +
sFilename + ".txt" );
}
catch( Exception e )
{
System.Windows.Forms.MessageBox.Show( e.ToString() );
fs = null;
}


Any ideas? Is this a Vista thing? I don't understand why it is so
difficult to do this right now (and I'm really suspecting Vista).

The scenario I need to work is this:

Any user logs in. My app starts running. The user does something and
my app will need to produce some logging. I check to see a directory
exists in my apps "Program Files" location. If not, the user needs to
be able to create it. All users will need to be able to write to this
directory. (I can't get directory creation to work either, but one
thing at a time--and I assume the solution will fix both problems.)
It will be a general logging location. The creation of the directory
is still important, because if it happends to get deleted, I want to
try to recreate it, not just be sunk for logging. And since I
wouldn't doubt one my clients could accidentally delete this folder, I
have to make it work in code.

Thanks,

Chad
 
I

Ignacio Machin ( .NET/ C# MVP )

I'm trying to do something extremely basic, and I don't remember
having these problems in XP.

I'm running a service, logged in as an administrator.  I created a
directory in windows explorer (as administrator), and set all
permissions/security for all users (including the Everyone group) to
full control.

The following code still does not work:

            FileStream fs;
            try
            {

System.Windows.Forms.MessageBox.Show( this.sFileStoreLocation + "\\" +
sFilename + ".txt" );
                fs = File.Create( this.sFileStoreLocation + "\\" +
sFilename + ".txt" );
            }
            catch( Exception e )
            {
                System.Windows.Forms.MessageBox.Show( e.ToString() );
                fs = null;
            }

Any ideas?  Is this a Vista thing?  I don't understand why it is so
difficult to do this right now (and I'm really suspecting Vista).

The scenario I need to work is this:

Any user logs in.  My app starts running.  The user does something and
my app will need to produce some logging.  I check to see a directory
exists in my apps "Program Files" location.  If not, the user needs to
be able to create it.  All users will need to be able to write to this
directory.  (I can't get directory creation to work either, but one
thing at a time--and I assume the solution will fix both problems.)
It will be a general logging location.  The creation of the directory
is still important, because if it happends to get deleted, I want to
try to recreate it, not just be sunk for logging.  And since I
wouldn't doubt one my clients could accidentally delete this folder, I
have to make it work in code.

Thanks,

Chad

Hi

where this.sFileStoreLocation is pointing to?

And yes, it's possible it's a Vista "feature"
 
W

Willy Denoyette [MVP]

thephatp said:
I'm trying to do something extremely basic, and I don't remember
having these problems in XP.

I'm running a service, logged in as an administrator. I created a
directory in windows explorer (as administrator), and set all
permissions/security for all users (including the Everyone group) to
full control.

The following code still does not work:

FileStream fs;
try
{

System.Windows.Forms.MessageBox.Show( this.sFileStoreLocation + "\\" +
sFilename + ".txt" );
fs = File.Create( this.sFileStoreLocation + "\\" +
sFilename + ".txt" );
}
catch( Exception e )
{
System.Windows.Forms.MessageBox.Show( e.ToString() );
fs = null;
}


Any ideas? Is this a Vista thing? I don't understand why it is so
difficult to do this right now (and I'm really suspecting Vista).

The scenario I need to work is this:

Any user logs in. My app starts running. The user does something and
my app will need to produce some logging. I check to see a directory
exists in my apps "Program Files" location. If not, the user needs to
be able to create it. All users will need to be able to write to this
directory. (I can't get directory creation to work either, but one
thing at a time--and I assume the solution will fix both problems.)
It will be a general logging location. The creation of the directory
is still important, because if it happends to get deleted, I want to
try to recreate it, not just be sunk for logging. And since I
wouldn't doubt one my clients could accidentally delete this folder, I
have to make it work in code.

Thanks,

Chad


What's the sFileStoreLocation value?
Keep in mind that "administrator" (like all administrators account!) is just
a regular user account , unless you run elevated (which is not the case
here).

Also, using MessageBox.Show (or whatever System;Windows.Forms class method)
is a big NO NO in Vista, if you really need to display a (simple) message
box, you will have to PInvoke the WTSSendMessage API.

Willy.
 
T

thephatp

What's the sFileStoreLocation value?
Keep in mind that "administrator" (like all administrators account!) is just
a regular user account , unless you run elevated (which is not the case
here).

Also, using MessageBox.Show (or whatever System;Windows.Forms class method)
is a big NO NO in Vista, if you really need to display a (simple) message
box, you will have to PInvoke the WTSSendMessage API.

Willy.- Hide quoted text -

- Show quoted text -

The sFileStoreLocation is pointing to a new folder that I created,
like "C:\Temp_NEW\MyFolder\". I've given all groups full permissions
on this folder, and yet I still get an error. I could check each
user's temp directory, but I have two issues with that. In Vista, I'm
getting that the path is:

"C:\Users\MyUser\AppData\Local\Microsoft\Windows\Temporary Internet
Files\Virtualized\C\Users\MyUser\AppData\Local\Temp\Low\"


I'm not familiar with virtualized folders, but I went to Temporary
Internet Files and got what I expected (no `Virtualized` folder ;),
but I also went to `C\Users\MyUser\AppData\Local\Temp\Low\`, but I
didn't see anything there either. So, knowing nothing about this is
my first problem. My second problem is that this would mean that I
have to check all user locations for files stored here. Oh, and I'll
want to create my own folder under this, like `MyAppLogs` so that I
can check them on the fly (in service). That's a real pain to have to
do that for each user. I'd like to be able to create a place where
all users can write to, and if the directory happens to get deleted,
that any user can recreate. However, if necessary, I could have the
background service do so (which would be running as system).

Any help? FYI, I'm using the System.IO.Path.GetTempPath() method.

Thanks again.
 
W

Willy Denoyette [MVP]

What's the sFileStoreLocation value?
Keep in mind that "administrator" (like all administrators account!) is
just
a regular user account , unless you run elevated (which is not the case
here).

Also, using MessageBox.Show (or whatever System;Windows.Forms class
method)
is a big NO NO in Vista, if you really need to display a (simple) message
box, you will have to PInvoke the WTSSendMessage API.

Willy.- Hide quoted text -

- Show quoted text -

The sFileStoreLocation is pointing to a new folder that I created,
like "C:\Temp_NEW\MyFolder\". I've given all groups full permissions
on this folder, and yet I still get an error. I could check each
user's temp directory, but I have two issues with that. In Vista, I'm
getting that the path is:

"C:\Users\MyUser\AppData\Local\Microsoft\Windows\Temporary Internet
Files\Virtualized\C\Users\MyUser\AppData\Local\Temp\Low\"


I'm not familiar with virtualized folders, but I went to Temporary
Internet Files and got what I expected (no `Virtualized` folder ;),
but I also went to `C\Users\MyUser\AppData\Local\Temp\Low\`, but I
didn't see anything there either. So, knowing nothing about this is
my first problem. My second problem is that this would mean that I
have to check all user locations for files stored here. Oh, and I'll
want to create my own folder under this, like `MyAppLogs` so that I
can check them on the fly (in service). That's a real pain to have to
do that for each user. I'd like to be able to create a place where
all users can write to, and if the directory happens to get deleted,
that any user can recreate. However, if necessary, I could have the
background service do so (which would be running as system).

Any help? FYI, I'm using the System.IO.Path.GetTempPath() method.

Thanks again.


Normally all "users" should be able to create folders/files under the root,
which begs the question - are you *sure* you are getting an "Access Denied"
exception when trying to create a folder/file under the root ("C:\"), what
makes you so sure about it?
Don't run a Service as "Administrator", set your service to run in the
"Local Service" account, try to create a folder and a file, this should
work. If it doesn't check the security settings on the root.

Willy.





Willy.
 

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