Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6703255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:08:59+00:00 2026-05-26T07:08:59+00:00

I’m searching for the solution for hours, but the documentation being sparse, I’m unable

  • 0

I’m searching for the solution for hours, but the documentation being sparse, I’m unable to find what I need.

If I have a file or a directory, how can I, in C# with P/Invoke, duplicate the permissions (ie. the owner and SIDs with their permissions) from one file to another or from one directory to another, assuming that those files and directories are either on the same machine or the network controlled by the same Active Directory server?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:09:00+00:00Added an answer on May 26, 2026 at 7:09 am

    Here’s an example using GetNamedSecurityInfo and
    SetNamedSecurityInfo.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    using System.Security.AccessControl;
    
    namespace PermissionsExample
    {
        class Program
        {
            static void Main(string[] args)
            {
                string source = "C:\\source.txt";
                string dest = "C:\\dest.txt";
    
                string result = CopyPermissions(source, dest);
                if (!string.IsNullOrEmpty(result)) { Console.WriteLine(result); }
                else { Console.WriteLine("SUCCESS"); }
            }
    
            public static string CopyPermissions(string source_file, string dest_file)
            {
                string errmsg = string.Empty;
                IntPtr sidOwner = IntPtr.Zero;
                IntPtr sidOwnerDescriptor = IntPtr.Zero;
                IntPtr sidGroup = IntPtr.Zero;
                IntPtr sidGroupDescriptor = IntPtr.Zero;
                IntPtr dacl = IntPtr.Zero;
                IntPtr daclDescriptor = IntPtr.Zero;
                IntPtr sacl = IntPtr.Zero;
                try
                {
                    int result = GetNamedSecurityInfo(source_file, SE_OBJECT_TYPE.SE_FILE_OBJECT, SecurityInfos.DiscretionaryAcl, out sidOwner, out sidGroup, out dacl, out sacl, out daclDescriptor);
                    if (result != 0)
                    {
                        Win32Exception e = new Win32Exception(result);
                        errmsg = "ERROR: " + e.Message;
                        return errmsg;
                    }
                    result = GetNamedSecurityInfo(source_file, SE_OBJECT_TYPE.SE_FILE_OBJECT, SecurityInfos.Owner, out sidOwner, out sidGroup, out dacl, out sacl, out sidGroupDescriptor);
                    if (result != 0)
                    {
                        Win32Exception e = new Win32Exception(result);
                        errmsg = "ERROR: " + e.Message;
                        return errmsg;
                    }
    
                    result = GetNamedSecurityInfo(source_file, SE_OBJECT_TYPE.SE_FILE_OBJECT, SecurityInfos.Group, out sidOwner, out sidGroup, out dacl, out sacl, out sidGroupDescriptor);
                    if (result != 0)
                    {
                        Win32Exception e = new Win32Exception(result);
                        errmsg = "ERROR: " + e.Message;
                        return errmsg;
                    }
    
                    SecurityInfos info = SecurityInfos.DiscretionaryAcl | SecurityInfos.Group | SecurityInfos.Owner;
                    result = SetNamedSecurityInfo(dest_file, SE_OBJECT_TYPE.SE_FILE_OBJECT, info, sidOwner, sidGroup, dacl, sacl);
                    if (result != 0)
                    {
                        Win32Exception e = new Win32Exception(result);
                        errmsg = "ERROR: " + e.Message;
                        return errmsg;
                    }
                }
                finally
                {
                    if (sidOwnerDescriptor != IntPtr.Zero && LocalFree(sidOwnerDescriptor) != IntPtr.Zero)
                    {
                        int err = Marshal.GetLastWin32Error();
                        Win32Exception e = new Win32Exception(err);
                        errmsg += "ERROR: " + e.Message;
                    }
                    if (sidGroupDescriptor != IntPtr.Zero && LocalFree(sidGroupDescriptor) != IntPtr.Zero)
                    {
                        int err = Marshal.GetLastWin32Error();
                        Win32Exception e = new Win32Exception(err);
                        errmsg += "ERROR: " + e.Message;
                    }
                    if (daclDescriptor != IntPtr.Zero && LocalFree(daclDescriptor) != IntPtr.Zero)
                    {
                        int err = Marshal.GetLastWin32Error();
                        Win32Exception e = new Win32Exception(err);
                        errmsg += "ERROR: " + e.Message;
                    }
                }
    
                return errmsg;
            }
    
            public enum SE_OBJECT_TYPE
            {
                SE_UNKNOWN_OBJECT_TYPE = 0,
                SE_FILE_OBJECT,
                SE_SERVICE,
                SE_PRINTER,
                SE_REGISTRY_KEY,
                SE_LMSHARE,
                SE_KERNEL_OBJECT,
                SE_WINDOW_OBJECT,
                SE_DS_OBJECT,
                SE_DS_OBJECT_ALL,
                SE_PROVIDER_DEFINED_OBJECT,
                SE_WMIGUID_OBJECT,
                SE_REGISTRY_WOW64_32KEY
            }
    
            [DllImport("advapi32.dll", EntryPoint = "GetNamedSecurityInfoW", ExactSpelling = true, CharSet = CharSet.Unicode)]
            private static extern int GetNamedSecurityInfo(string objectName, SE_OBJECT_TYPE objectType,
                System.Security.AccessControl.SecurityInfos securityInfo, out IntPtr sidOwner,
                out IntPtr sidGroup, out IntPtr dacl, out IntPtr sacl, out IntPtr securityDescriptor);
    
            [DllImport("advapi32.dll", EntryPoint = "SetNamedSecurityInfoW", ExactSpelling = true, CharSet = CharSet.Unicode)]
            private static extern int SetNamedSecurityInfo(string objectName, SE_OBJECT_TYPE objectType,
                System.Security.AccessControl.SecurityInfos securityInfo, IntPtr sidOwner,
                IntPtr sidGroup, IntPtr dacl, IntPtr sacl);
    
            [DllImport("kernel32.dll", EntryPoint = "LocalFree", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode)]
            private static extern IntPtr LocalFree(IntPtr hMem);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.