C# Get Owner of File with Long File Path

I couldn’t find a good example of how to get the file owner of a file from a long file path. The Delimon class doesn’t support the GetAccessControl method from FileInfo.

 

//Add Reference to Win32Security.dll from https://github.com/woanware/Win32Security/ 
//Add Reference to Delimon Class https://gallery.technet.microsoft.com/scriptcenter/DelimonWin32IO-Library-V40-7ff6b16c
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

/*
Delimon.Win32.IO Class License
Copyright © 2012, Johan Delimon
http://bit.ly/delimon 
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/
namespace LongPaths
{
    public static class LongPathFiles
    {
        public static string WindowsPathClean(string strPath)
        {
            if (strPath != null)
            {
                if (strPath != "")
                {
                    strPath = strPath.Replace("/", "\\");
                    strPath = strPath.Replace("\\\\", "\\");
                    strPath = strPath.Replace("\\\\", "\\");

                    //Fix Empty Paths
                    if (strPath == "\\")
                    {
                        strPath = "";
                    }

                    if (strPath.Length > 2)
                    {
                        //Remove Ending BackSlashes from folders
                        if (strPath.Substring(strPath.Length - 2, 2) == "\\")
                        {
                            strPath = strPath.Remove(strPath.Length - 2);
                        }
                        //Fix Share Paths to have 2 beginning backslashes
                        if (strPath.Substring(0, 1) == "\\" && strPath.Substring(0, 2) != "\\\\")
                        {
                            strPath = "\\" + strPath;
                        }

                    }
                }
            }
            return strPath;
        }

        /// <summary>
        /// Converts Prepended Path to UNC Full Path or Local Drive Path
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string RemovePrependGetPath(string value)
        {
            value = value.Replace(@"\\?\UNC\", @"\\");
            value = value.Replace(@"\\?\", "");
            System.Uri uri = new System.Uri(value);

            return uri.LocalPath;
        }

        /// <summary>
        /// Prepend "\\?\" to drive path and "\\?\UNC\" to UNC path so that long file names can be handled without an error
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string LongPathPrepend(string value)
        {
            //Just in case the prepend was already added remove it.
            value = value.Replace(@"\\?\UNC\", @"\\");
            value = value.Replace(@"\\?\", "");

            //Remove the ending backslashes and fix any common problems with the path
            value = WindowsPathClean(value);

            string strPath = "";
            System.Uri uri = new System.Uri(value);
            if (uri.IsUnc)
            {
                //Add the prepend and remove the 2 beginning back slashes for the UNC path so that long file paths are supported
                strPath = @"\\?\UNC\" + uri.LocalPath.Substring(2, uri.LocalPath.Length - 2);
            }
            else
            {
                //Local path prepend with @"\\?\" so that long file paths are supported by the search.
                strPath = @"\\?\" + uri.LocalPath;
            }
            return strPath;
        }

        /// <summary>
        /// Get File Owner from FullFilePath works with long file paths
        /// </summary>
        /// <param name="strFileName"></param>
        /// <returns></returns>
        public static string GetFileOwner(string strFileName)
        {
            string strOwner = "";
            string strLongFilePath = LongPathPrepend(strFileName);
            Microsoft.Win32.Security.SecurityDescriptor secDesc = null;
            try
            {
                if (Delimon.Win32.IO.File.Exists(strFileName))
                {
                    secDesc = Microsoft.Win32.Security.SecurityDescriptor.GetFileSecurity(strLongFilePath, Microsoft.Win32.Security.SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION);
                    strOwner = secDesc.Owner.DomainName + "\\" + secDesc.Owner.AccountName;

                }

            }
            catch (Exception)
            {
                strOwner = "";
            }
            finally
            {
                if (secDesc != null)
                {
                    secDesc.Dispose();
                    secDesc = null;
                }
            }

            return strOwner;
        }
    }
}

Resources:

 

 

Leave a Reply

%d bloggers like this: