Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I want to get permission to interact with a file.

I want to get permission to interact with a file.

Scheduled Pinned Locked Moved C#
debuggingsecurityannouncementlounge
3 Posts 3 Posters 46 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yohan Bischofberger
    wrote on last edited by
    #1

    Hello, I want to work with a File. I cant get Permission or owner. Im not the owner of the files as i can see in the propertys. The Path of the Files are: %userprofile%\AppData\Local\Temp Here is how i tried to get access owner or permission:

    //Create FileInfo

    FileInfo info = new FileInfo(path);

    //Create FileSecurity

    FileSecurity security = FileSystemAclExtensions.GetAccessControl(info);

    //Create Privileges

    SecurityIdentifier identifier = WindowsIdentity.GetCurrent().User;

    //Set Owner

    security.SetOwner(identifier);

    //Set Access rules

    security.AddAccessRule(new FileSystemAccessRule(identifier, FileSystemRights.FullControl, AccessControlType.Allow));

    //Update Acces Controll on File

    FileSystemAclExtensions.SetAccessControl(info, security);

    My goal is to open a FileStream and Write over the File, here is a exaple:

    File.SetAttributes(_recycleBin, FileAttributes.Normal);
    string[] files = Directory.GetFiles(_recycleBin, "", System.IO.SearchOption.AllDirectories);

    foreach (string file in files)
    {
    try
    {
    // Set the files attributes to normal in case it's read-only.

         File.SetAttributes(file, FileAttributes.Normal);
    
         // Calculate the total number of sectors in the file.
         double sectors = Math.Ceiling(new FileInfo(file).Length / 512.0);
    
         // Create a dummy-buffer the size of a sector.
    
         byte\[\] dummyBuffer = new byte\[512\];
    
         // Create a cryptographic Random Number Generator.
         // This is what I use to create the garbage data.
    
         RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    
         Trace.WriteLine("DEBUG LOG: " + file + "loaded.");
    
         FileStream inputStream = new FileStream(file, FileMode.Open);
    
         for (int currentPass = 0; currentPass < timesToWrite; currentPass++)
         {
             //UpdatePassInfo(currentPass + 1, timesToWrite);
    
             // Go to the beginning of the stream
    
             inputStream.Position = 0;
    
             // Loop all sectors
             for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++)
             {
                 //UpdateSectorInfo(sectorsWritten + 1, (int)sectors);
    
                 // Fill the dummy-buffer with random data
    
                 rng.GetBytes(dummyBuffer);
    
                 // Write it to the stream
                 inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
             }
    
             //Delete
    
    L C 2 Replies Last reply
    0
    • Y Yohan Bischofberger

      Hello, I want to work with a File. I cant get Permission or owner. Im not the owner of the files as i can see in the propertys. The Path of the Files are: %userprofile%\AppData\Local\Temp Here is how i tried to get access owner or permission:

      //Create FileInfo

      FileInfo info = new FileInfo(path);

      //Create FileSecurity

      FileSecurity security = FileSystemAclExtensions.GetAccessControl(info);

      //Create Privileges

      SecurityIdentifier identifier = WindowsIdentity.GetCurrent().User;

      //Set Owner

      security.SetOwner(identifier);

      //Set Access rules

      security.AddAccessRule(new FileSystemAccessRule(identifier, FileSystemRights.FullControl, AccessControlType.Allow));

      //Update Acces Controll on File

      FileSystemAclExtensions.SetAccessControl(info, security);

      My goal is to open a FileStream and Write over the File, here is a exaple:

      File.SetAttributes(_recycleBin, FileAttributes.Normal);
      string[] files = Directory.GetFiles(_recycleBin, "", System.IO.SearchOption.AllDirectories);

      foreach (string file in files)
      {
      try
      {
      // Set the files attributes to normal in case it's read-only.

           File.SetAttributes(file, FileAttributes.Normal);
      
           // Calculate the total number of sectors in the file.
           double sectors = Math.Ceiling(new FileInfo(file).Length / 512.0);
      
           // Create a dummy-buffer the size of a sector.
      
           byte\[\] dummyBuffer = new byte\[512\];
      
           // Create a cryptographic Random Number Generator.
           // This is what I use to create the garbage data.
      
           RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
      
           Trace.WriteLine("DEBUG LOG: " + file + "loaded.");
      
           FileStream inputStream = new FileStream(file, FileMode.Open);
      
           for (int currentPass = 0; currentPass < timesToWrite; currentPass++)
           {
               //UpdatePassInfo(currentPass + 1, timesToWrite);
      
               // Go to the beginning of the stream
      
               inputStream.Position = 0;
      
               // Loop all sectors
               for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++)
               {
                   //UpdateSectorInfo(sectorsWritten + 1, (int)sectors);
      
                   // Fill the dummy-buffer with random data
      
                   rng.GetBytes(dummyBuffer);
      
                   // Write it to the stream
                   inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
               }
      
               //Delete
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Yohan Bischofberger wrote:

      My goal is ...

      But what is your question?

      1 Reply Last reply
      0
      • Y Yohan Bischofberger

        Hello, I want to work with a File. I cant get Permission or owner. Im not the owner of the files as i can see in the propertys. The Path of the Files are: %userprofile%\AppData\Local\Temp Here is how i tried to get access owner or permission:

        //Create FileInfo

        FileInfo info = new FileInfo(path);

        //Create FileSecurity

        FileSecurity security = FileSystemAclExtensions.GetAccessControl(info);

        //Create Privileges

        SecurityIdentifier identifier = WindowsIdentity.GetCurrent().User;

        //Set Owner

        security.SetOwner(identifier);

        //Set Access rules

        security.AddAccessRule(new FileSystemAccessRule(identifier, FileSystemRights.FullControl, AccessControlType.Allow));

        //Update Acces Controll on File

        FileSystemAclExtensions.SetAccessControl(info, security);

        My goal is to open a FileStream and Write over the File, here is a exaple:

        File.SetAttributes(_recycleBin, FileAttributes.Normal);
        string[] files = Directory.GetFiles(_recycleBin, "", System.IO.SearchOption.AllDirectories);

        foreach (string file in files)
        {
        try
        {
        // Set the files attributes to normal in case it's read-only.

             File.SetAttributes(file, FileAttributes.Normal);
        
             // Calculate the total number of sectors in the file.
             double sectors = Math.Ceiling(new FileInfo(file).Length / 512.0);
        
             // Create a dummy-buffer the size of a sector.
        
             byte\[\] dummyBuffer = new byte\[512\];
        
             // Create a cryptographic Random Number Generator.
             // This is what I use to create the garbage data.
        
             RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        
             Trace.WriteLine("DEBUG LOG: " + file + "loaded.");
        
             FileStream inputStream = new FileStream(file, FileMode.Open);
        
             for (int currentPass = 0; currentPass < timesToWrite; currentPass++)
             {
                 //UpdatePassInfo(currentPass + 1, timesToWrite);
        
                 // Go to the beginning of the stream
        
                 inputStream.Position = 0;
        
                 // Loop all sectors
                 for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++)
                 {
                     //UpdateSectorInfo(sectorsWritten + 1, (int)sectors);
        
                     // Fill the dummy-buffer with random data
        
                     rng.GetBytes(dummyBuffer);
        
                     // Write it to the stream
                     inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
                 }
        
                 //Delete
        
        C Offline
        C Offline
        charles henington
        wrote on last edited by
        #3

        You're going to need to add a manifest to your project. Add -> Item -> Application Manifest File. Change the line that says requestedExecutionLevel from level="asInvoker" to level="requireAdministrator"

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups