locked
Access Denied exception while trying to delete or create file. RRS feed

  • Question

  • Hi everybody

    i tired this code 

                If System.IO.File.Exists(Application.StartupPath & "\pattern.dat") = True Then

                    System.IO.File.Delete(Application.StartupPath & "\pattern.dat")

                End If

    but this code not working on windows vista, i can use it on windows xp, i think problem is user permissions but i have to delete or create file via my application how can resolve this problem.

    Thanks for your answer.

    Saturday, April 10, 2010 7:39 AM

Answers

  • It is protected that you cannot delete anymore files from the program files folder (changing uac is only a short solution in Windows 7 this is even more strict)

    You have to use another folder for that

    http://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=VS.100).aspx

    Be aware the above is for 2010

     


    Success
    Cor
    • Marked as answer by Jeff Shan Friday, April 16, 2010 7:15 AM
    Saturday, April 10, 2010 11:50 AM
  •  

    If you're saying about "This application requires Admin privilege" dialog that appears on each application start, its not a problem, its a feature.

    as cor has suggested consider using different directory to store data if possible. Take a look at  My.Computer.FileSystem.SpecialDirectories object.

     


    Thanks

    "Feel the Force !"
    • Marked as answer by Jeff Shan Friday, April 16, 2010 7:15 AM
    Saturday, April 10, 2010 6:03 PM

All replies

  • you need to make your application UAC compliant so that it will ask user to grant your application Administrator access.

    to make your application UAC compliant,

    Double Click My Project in Solution Explorer and Click UAC Settings button as shown in image.




    Then Modify app.manifest file as shown below. Make it 'requireAdministrator' instead of 'asInvoker' and save.





    Thanks

    "Feel the Force !"
    • Proposed as answer by Faraz Azher Wednesday, November 24, 2010 9:05 AM
    Saturday, April 10, 2010 8:31 AM
  • Hi,

    This will not work if your using Express version of VS. I tried this on one of my applications because of the same problem and VB Express would not allow it.

     But a work around for this is turn off the UAC in the user accounts. But be causiuos using this because it disables a lot of security checks in vista.

     Why they don't allow Express to use this I don't know Probably just for a selling point for the full version.

    Curtis


    Always Lost in Code, Always mark answers as correct if they answer you question and solve your problem. This way others when searching for similar problems can find the answer faster.
    Saturday, April 10, 2010 11:00 AM
  • It is protected that you cannot delete anymore files from the program files folder (changing uac is only a short solution in Windows 7 this is even more strict)

    You have to use another folder for that

    http://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=VS.100).aspx

    Be aware the above is for 2010

     


    Success
    Cor
    • Marked as answer by Jeff Shan Friday, April 16, 2010 7:15 AM
    Saturday, April 10, 2010 11:50 AM
  • Dear Omie thanks for your answer 

    i replaced requestedExecutionLevel type but now i get error message like this "ClickOnce does not support the request execution level 'requireAdministrator'" how can resolve this problem, i am using Visual Studio 2008 Enterprise Edition i replaced with this <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" /> is it true ? and what is the problem ?

    Saturday, April 10, 2010 4:38 PM
  • BlueEffect,

     They changed the security the security on vista and windows 7. They have restricted access to the program and system directories. As far as I can tell by what I have read and by what the MVP's have told me there is not a way to access those directories. I believe they are read only. You can use the speacial directories that are in the following path C:\users\"USERname"\Documents\. This is the path of the speacial directory. But I have been able to create and use a directory at c:\ . The root directory and any subdirectories you create are usable. Or at least the program I wrote making a directory on C:\ still installs and works.

    Curtis


    Always Lost in Code, Always mark answers as correct if they answer you question and solve your problem. This way others when searching for similar problems can find the answer faster.
    Saturday, April 10, 2010 5:49 PM
  • I resolved this problem but there is a new problem when disabled ClickOnce properties Windows vista want to authorization from user each restart application i have to solve this problem :( 
    Saturday, April 10, 2010 5:49 PM
  • BlueEffect,

     They changed the security the security on vista and windows 7. They have restricted access to the program and system directories. As far as I can tell by what I have read and by what the MVP's have told me there is not a way to access those directories. I believe they are read only. You can use the speacial directories that are in the following path C:\users\"USERname"\Documents\. This is the path of the speacial directory. But I have been able to create and use a directory at c:\ . The root directory and any subdirectories you create are usable. Or at least the program I wrote making a directory on C:\ still installs and works.

    Curtis


    Always Lost in Code, Always mark answers as correct if they answer you question and solve your problem. This way others when searching for similar problems can find the answer faster.
    Saturday, April 10, 2010 5:49 PM
  •  

    If you're saying about "This application requires Admin privilege" dialog that appears on each application start, its not a problem, its a feature.

    as cor has suggested consider using different directory to store data if possible. Take a look at  My.Computer.FileSystem.SpecialDirectories object.

     


    Thanks

    "Feel the Force !"
    • Marked as answer by Jeff Shan Friday, April 16, 2010 7:15 AM
    Saturday, April 10, 2010 6:03 PM
  • Curtis Un Thanks for your answer but some applications can access all directories and files by and they can write,delete or change for example antivirus applications they can do this, i am sure there is a resolve for this problem. have to be.  
    Saturday, April 10, 2010 7:49 PM
  •  Blue the problem is that those applications you are referring to use C++ or some other language that does not require .net or uses unstructured language.  VB.net is a structured language and there are some things that cannot be done such as hooking the - key from the keyboard object with a hidden background operation. Now there may be a third party installer such as inno (at least I think that is the name) that may be able to do it using the code written in VB. I did publish an app using the the administrator level but it published once and I never got it to publish again using it. I don't know exactly what happen, why or how. That was back before I was told that it would not work in VB express. I don't know about the full version of VS, the commands must be there for a reason or they were there for XP. I have found that using a root subdirectory that I create for my apps saves and loads does just as well. And I don't have to worry about the permissions. I also agree with them that the program and system files should be off limits.

    Curtis


    Always Lost in Code, Always mark answers as correct if they answer you question and solve your problem. This way others when searching for similar problems can find the answer faster.
    Saturday, April 10, 2010 9:25 PM
  • I find this really odd. I swear I've never had a problem with this but maybe I've just lucked up?

    At any rate, please try this (even though I don't think it'll make a bit of difference):

            Dim fileToFind As String = Application.StartupPath & "\pattern.dat"
    
            If My.Computer.FileSystem.FileExists(fileToFind) Then
                My.Computer.FileSystem.DeleteFile(fileToFind)
            End If

     

    That's nothing more than a shortcut to what you're doing so if yours isn't working, I can't imagine why the above would make a difference. I will add this though: I had the Express edition for maybe two weeks when I bought and installed the Standard edition - maybe Curtis is right about it being somehow "crippled".

    Good luck with it!

    :)

    Sunday, April 11, 2010 12:46 AM
  • Just to be sure of something here:

    Your post says something about "...or creating a file", but the code you posted only shows the part about deleting it.

    Are you sure that you have [whatever you're doing to create it] closed and/or disposed? One thing I had to teach myself to do "as an always no matter what" was when using a text reader, read it, then close it! It caused me endless grief until I figured that out, so let's please be sure that the file you're trying to delete isn't open by something else in your program.

    Sunday, April 11, 2010 12:55 AM
  •  Frank I read the click once again and the way it say is it automatically sets the permissions level in one place and in another it says the administrator sets it. Very confusing. It says nothing about it being lower for VB express. I have a label app that I wrote back in 2008 that uses writeline and readline to save and load files written to C:\Labels\Logs. It still works correctly but if  I change directory to c:\programs\ it will give a permissions error in Vista. If i remember correctly in XP it did not do this. This the link

    http://msdn.microsoft.com/en-us/library/76e4d2xw(VS.80).aspx

    Curtis

     


    Always Lost in Code, Always mark answers as correct if they answer you question and solve your problem. This way others when searching for similar problems can find the answer faster.
    Sunday, April 11, 2010 1:32 AM
  • @ Curtis ->

    Well ain't that just plain weird then!

    I've never used the ClickOnce deal - I have my own installer that I prefer.

    Sunday, April 11, 2010 2:35 AM
  • Hi Frank L. Smith 

     

    i tired your code but result is same "permission denied" :(

     

    can i resolve non trusted application warning message problem by Installshield ? dou you have a suggestion ?

    Sunday, April 11, 2010 8:44 AM
  • Did you read what Omnie and I wrote, or do you want to something you want, but which is impossible.

    You should know what I want, but am not able to do.

     


    Success
    Cor
    Sunday, April 11, 2010 9:40 AM
  •  Cor Lighter, i changed app.manifest and i resolved file access problem it is done, but now windows vista want to approve for run my application, i think i have to register my application as truster apllication
    Sunday, April 11, 2010 12:36 PM
  • You can probably install it now, but a part was that you wanted to be able to remove it from inside your applications, which maybe will go at Vista, but you are in trouble again in Windows 7
    Success
    Cor
    Sunday, April 11, 2010 1:45 PM

  • This works beautifully for me !
    you need to make your application UAC compliant so that it will ask user to grant your application Administrator access.

    to make your application UAC compliant,

    Double Click My Project in Solution Explorer and Click UAC Settings button as shown in image.




    Then Modify app.manifest file as shown below. Make it 'requireAdministrator' instead of 'asInvoker' and save.





    Thanks

    "Feel the Force !"

    Wednesday, November 24, 2010 9:05 AM
  • Note to all,

     I now have the full version of VS Ultimate and it works as it should so the Express version is crippled. I used Express version for a 4 of years and just used the documents directory for files since the change in security. I had to go back and rewrite a couple of applications because of this. I don't understand MS logic on this unless it is to give the upper hand to developers with the full version and promote sales. But why don't they just say so? I have found nothing yet indicating this.

    Curtis


    Always Lost in Code, Always mark answers as correct if they answer you question and solve your problem. This way others when searching for similar problems can find the answer faster.
    Friday, November 26, 2010 2:04 PM