none
Delete a file even if hte program has no permission ! RRS feed

  • Question

  • Hi,

    I have a program that needs to delete a file, and download a new one from the internet.
    This is working fine on windows xp, but on windows vista it does not delete the file... I think it's a permission program, an the program needs to do it automatically without the user intervention... is there a way to do that ? Like to force to delete the file even if the program can't ?

    I've already killed the process, and there is no way to delete the file... (I also tried to use delegate...It helped for another problem...)

    Thanks
    http://zopostyle.com | http://brunozp.com
    Tuesday, November 11, 2008 10:36 PM

Answers

  •  
            public static void setAccessRule ( string directory )  
            {  
                System.Security.AccessControl.DirectorySecurity sec =  
                                        System.IO.Directory.GetAccessControl ( directory );  
                FileSystemAccessRule accRule = new FileSystemAccessRule ( Globals.userIdentity,  
                     FileSystemRights.FullControl, AccessControlType.Allow );  
                sec.AddAccessRule ( accRule );  
            }                                               // setACL 

    AlexB
    • Proposed as answer by Rahul P Nath Wednesday, November 12, 2008 4:16 AM
    • Marked as answer by BrunoZ Wednesday, November 12, 2008 8:25 PM
    • Unmarked as answer by BrunoZ Wednesday, November 12, 2008 8:59 PM
    • Marked as answer by BrunoZ Wednesday, November 12, 2008 11:03 PM
    Wednesday, November 12, 2008 1:52 AM
  • UserIdentity can be had as follows:

    string userIdentity = Environment.UserDomainName +  Environment.UserName;  

    AlexB
    • Marked as answer by BrunoZ Wednesday, November 12, 2008 11:03 PM
    Wednesday, November 12, 2008 10:48 PM

All replies

  • It is very easy if the user has administrator's priviledges. You will have to set ACL rule for that. I can post a routine later.
    AlexB
    Wednesday, November 12, 2008 1:24 AM
  • So, please...
    The user has always admin privileges.
    http://zopostyle.com | http://brunozp.com
    Wednesday, November 12, 2008 1:25 AM
  •  
            public static void setAccessRule ( string directory )  
            {  
                System.Security.AccessControl.DirectorySecurity sec =  
                                        System.IO.Directory.GetAccessControl ( directory );  
                FileSystemAccessRule accRule = new FileSystemAccessRule ( Globals.userIdentity,  
                     FileSystemRights.FullControl, AccessControlType.Allow );  
                sec.AddAccessRule ( accRule );  
            }                                               // setACL 

    AlexB
    • Proposed as answer by Rahul P Nath Wednesday, November 12, 2008 4:16 AM
    • Marked as answer by BrunoZ Wednesday, November 12, 2008 8:25 PM
    • Unmarked as answer by BrunoZ Wednesday, November 12, 2008 8:59 PM
    • Marked as answer by BrunoZ Wednesday, November 12, 2008 11:03 PM
    Wednesday, November 12, 2008 1:52 AM
  • OK,

    I'm gonna test...but when I compile...I get this error: Error 1 The name 'Globals' does not exist in the current context C:\Users\Bruno\Documents\Visual Studio 2008\Projects\Atualizador\renomeia\Program.cs 41 69 renomeia

    Thanks
    http://zopostyle.com | http://brunozp.com
    Wednesday, November 12, 2008 9:00 PM
  • BrunoZ said:

    OK,

    I'm gonna test...but when I compile...I get this error: Error 1 The name 'Globals' does not exist in the current context C:\Users\Bruno\Documents\Visual Studio 2008\Projects\Atualizador\renomeia\Program.cs 41 69 renomeia

    Thanks


    http://zopostyle.com | http://brunozp.com



    Well, Globals is jiust my personal custom static class. It contains static properties relevant to my project. Obviously you must remove it. Adjust the routine to yuour reality. UserIdentity is computerName(DomainName) + UserName. Do you want code to retrieve that as well?
    AlexB
    Wednesday, November 12, 2008 10:45 PM
  • UserIdentity can be had as follows:

    string userIdentity = Environment.UserDomainName +  Environment.UserName;  

    AlexB
    • Marked as answer by BrunoZ Wednesday, November 12, 2008 11:03 PM
    Wednesday, November 12, 2008 10:48 PM
  • Man, you rock...lol..

    Just a comment... it's by.. 
    string userIdentity = Environment.UserDomainName + "\\" + Environment.UserName;  


    Thanks
    http://zopostyle.com | http://brunozp.com
    • Edited by BrunoZ Wednesday, November 12, 2008 11:04 PM
    Wednesday, November 12, 2008 11:02 PM
  • BrunoZ said:

    Man, you rock...lol..

    Just a comment... it's by.. 

    string userIdentity = Environment.UserDomainName + "\\" + Environment.UserName;  


    Thanks
    http://zopostyle.com | http://brunozp.com



    Yep, I apologize for that. I actually realized my mistake an hour later when I was already in bed but decided not to get up and correct until this morning. You see, years ago I was in your shoes and could not figure out elementary things and everything was grinding at frustratingly slow pace. Now I tend to forget how it was and my references become sloppy. It is not easy for me to put myself in your shoes anymore. You will be in my shoes eventually. I wrote that from memory.
    AlexB
    Thursday, November 13, 2008 12:53 PM