none
Inconsistency to Get file attributes of file shared on SMB RRS feed

  • Question

  • Hi,

    We are trying to enumerate the files and check the file attributes of file.

    We have used DirectoryInfo, GetFileAttributes().

    But the result is returned as 0 instead of FileAttributes.

    Is there any approach to get the file attributes of files and folders shared on SMB share.

    Thank you,

    Saturday, May 5, 2018 1:48 AM

Answers

  • Hello Neil,

    Thank you for your suggestions and redirecting us to use Shell API.

    The issue is resoved by using SHGetFileInfo() Shell API.

    Sample code for reference.

    //declaration
    [DllImport(shell32_dll, BestFitMapping = false, ThrowOnUnmappableChar = true)]
    public static extern int SHGetFileInfo(string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, SHGFI_Flag uFlags);
    
    
    //call it in background thread to avoid UI freeze
    private uint GetAttributes(string fileName)
    {
     uint uiAttributes = 0;
     try
     {
         //call it in background thread to avoid UI freeze
         SHGFI_Flag flags;
         NativeMethods.SHFILEINFO info = new NativeMethods.SHFILEINFO();
         info.szDisplayName = String.Empty;
         info.szTypeName = String.Empty;
    
         flags = (SHGFI_Flag.SHGFI_ATTRIBUTES);
         int attributes = 0; 
         NativeMethods.SHGetFileInfo(fileName, attributes, ref info, Marshal.SizeOf(info), flags);
         uiAttributes = info.dwAttributes;
     }
     catch (Exception ex)
     {
           //execute exception handling
    
     }
     return uiAttributes;
    }
    With the above code, the issue is resolved.

    Thank you very much..

    Regards,


    • Edited by sharp1257 Wednesday, July 18, 2018 8:01 AM formated for clarity
    • Marked as answer by sharp1257 Wednesday, July 18, 2018 8:02 AM
    Wednesday, July 18, 2018 7:55 AM

All replies

  • Hello sharp1257,

    Did you check the below link?

    https://msdn.microsoft.com/en-us/library/ee441431.aspx?f=255&MSPPError=-2147217396

    Hope this would be helpful.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 7, 2018 5:46 AM
    Moderator
  • Hello Neil,

    Thank you very much for your reply and the link.

    >>https://msdn.microsoft.com/en-us/library/ee441431.aspx?f=255&MSPPError=-2147217396

    Is there any API which implements the above so that we can get consistent result.

    Note: We have tried DirectoryInfo and GetFileAttributes() which are returning '0' inconsistently.

    Regards,

    Monday, May 7, 2018 6:34 AM
  • Hello sharp1257,

    As far as I search, I didn't find relating API but you could try use Shell32 dll (Like run command in DOS )to get file attribute as below code.(not test on SMB).

     [STAThread]
            static void Main(string[] args)
            {
                // File path to property  
                string filePath = "D:\\WinRar\\7zxa.dll";
                // Initialization Shell Interface  
                Shell32.Shell shell = new Shell();
                // Get the parent directory object  
                var fName = filePath.Substring(0, filePath.LastIndexOf("\\"));
                Folder folder = shell.NameSpace(fName);
                // Access to files FolderItem object  
                var item= filePath.Substring(filePath.LastIndexOf("\\") + 1);
                FolderItem item = folder.ParseName(item);
                // The dictionary is stored attribute names and values of the key  
                List<KeyValuePair<string, string>> Properties = new List<KeyValuePair<string, string>>();
                
                for(int i=0;i<short.MaxValue;i++)
                {
                    // Gets the name of the attribute  
                    string key = folder.GetDetailsOf(null, i);
                    if (key=="Attributes")
                    {
                        Console.WriteLine(folder.GetDetailsOf(item, i));                 
                        break;
                    }
                    // Get attribute values             
                }
                Console.ReadLine();

    And the return value could check from below link.

    Understanding Windows File Attributes

    Hope this would be helpful.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 8, 2018 9:11 AM
    Moderator
  • Hello Neil,

    Thank you for proposing an alternate solution.

    I will test your solution with SMB and update the status here.

    Regards,

    • Marked as answer by sharp1257 Wednesday, July 18, 2018 8:01 AM
    • Unmarked as answer by sharp1257 Wednesday, July 18, 2018 8:02 AM
    Thursday, May 31, 2018 6:47 AM
  • Hello Neil,

    Thank you for your suggestions and redirecting us to use Shell API.

    The issue is resoved by using SHGetFileInfo() Shell API.

    Sample code for reference.

    //declaration
    [DllImport(shell32_dll, BestFitMapping = false, ThrowOnUnmappableChar = true)]
    public static extern int SHGetFileInfo(string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, SHGFI_Flag uFlags);
    
    
    //call it in background thread to avoid UI freeze
    private uint GetAttributes(string fileName)
    {
     uint uiAttributes = 0;
     try
     {
         //call it in background thread to avoid UI freeze
         SHGFI_Flag flags;
         NativeMethods.SHFILEINFO info = new NativeMethods.SHFILEINFO();
         info.szDisplayName = String.Empty;
         info.szTypeName = String.Empty;
    
         flags = (SHGFI_Flag.SHGFI_ATTRIBUTES);
         int attributes = 0; 
         NativeMethods.SHGetFileInfo(fileName, attributes, ref info, Marshal.SizeOf(info), flags);
         uiAttributes = info.dwAttributes;
     }
     catch (Exception ex)
     {
           //execute exception handling
    
     }
     return uiAttributes;
    }
    With the above code, the issue is resolved.

    Thank you very much..

    Regards,


    • Edited by sharp1257 Wednesday, July 18, 2018 8:01 AM formated for clarity
    • Marked as answer by sharp1257 Wednesday, July 18, 2018 8:02 AM
    Wednesday, July 18, 2018 7:55 AM