none
C# extended file properties- null reference RRS feed

  • Question

  • Hi,

    I've added reference to Shell Controls and Automation, manually added reference to Windows/System32/shell32.dll, but I cannot access FolderItem interface. here is my code:

    using Shell32; namespace MyApp.FileManager { public static class FileManager_Methods { public static void ShowDirectory(ListView Lview, string Path) {

    Shell shell= new Shell();

    Folder folder_prop= shell.NameSpace(Path);

            FolderItem prop;

    Lview.Items.Clear();

            dynamic dir_items = new DirectoryInfo(Path);
          List<ListViewItem> items = new List<ListViewItem>();
            Dictionary<string, string> dicTypes = new Dictionary<string, string>();

                foreach (DirectoryInfo dir in dir_items.EnumerateDirectories())
                {
                  ListViewItem lvi = new ListViewItem();
               
                    lvi.Tag = dir.FullName;
                    lvi.Text = dir.Name;

                    if (!dicTypes.ContainsKey(dir.Extension))
                    {
                       prop = folder_prop.ParseName(Path.GetFileName(dir.FullName));
                        dicTypes.Add(dir.Extension, folder_prop.GetDetailsOf(prop, 2).Trim());
                       
                    }

                    lvi.ImageKey = "folder";

                    lvi.SubItems.Add(dir.LastWriteTime.ToShortDateString() + " " + dir.LastWriteTime.ToShortTimeString());
                    lvi.SubItems.Add(dicTypes[dir.Extension]);
                    items.Add(lvi);

                } } } }


     

    Error is in line  prop= folder_prop.... "object reference not set to an instance of an object". Thanks for help in advance !







    • Edited by LuckyLuke82 Monday, October 2, 2017 12:05 PM
    Monday, October 2, 2017 10:53 AM

Answers

  • Solved. My code was wrong. 

    Folder folder_prop= shell.NameSpace(Path);

    should be

    Folder folder_prop= shell.NameSpace(dir);
    so It should be placed inside loop of EnumerateDirectories. Not sure why same code works for me in VB.NET though.



    • Edited by LuckyLuke82 Tuesday, October 3, 2017 7:08 AM
    • Marked as answer by LuckyLuke82 Tuesday, October 3, 2017 7:08 AM
    Tuesday, October 3, 2017 7:05 AM

All replies

  • Hi LuckyLuke82,

    You didn't specify on which line or object getting error.

    As the error message saying, are you sure that the object property you are trying to access has value there?
    Or,

    instead of directly accessing put an if condition where if null then empty otherwise set value.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, October 2, 2017 11:08 AM
    Moderator
  • I have specified where error is, under my code. Debugger says It's a null value. And I can't check null condition on FolderItem, It gives me "Use of unassigend local variable" error.
    Monday, October 2, 2017 11:27 AM
  • Hi LuckyLuke82,

    Make sure you have value assign. Please put a debug pointer in the following and see if value is passing or not.

    prop = folder_prop.ParseName(Path.GetFileName(dir.FullName));


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, October 2, 2017 11:35 AM
    Moderator
  • Results from each object in that line is:

    prop : null

    folder_prop: null

    Path.GetFileName(dir.Fullname): "1.0"

    As told, shell objects have null references. I don't know why, with same code and same references everything works for me in VB.NET.

    Monday, October 2, 2017 11:57 AM
  • @LuckyLuke82 you use DirectoryInfo.FullName which returns the full path of the directory and Path.GetFileName which returns the file name and extension of the specified path string. But from the output you given "Path.GetFileName(dir.Fullname): "1.0"" I guess it is not correct based on the definition of the two method.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, October 2, 2017 12:34 PM
    Moderator
  • I'm not following you now. I think string is correct. "1.0" is the name of the folder, and It should extract property of It, specificaly in my Listview It should be listed as "folder". I don't think this is an issue. I tried to add string manually, like prop= "1.0" without Path.GetFileName or Fullname, and It didn't work also. Path.GetFullPath doesn't work either, ofcourse.

    • Edited by LuckyLuke82 Monday, October 2, 2017 12:58 PM
    Monday, October 2, 2017 12:55 PM
  • @LuckyLuke82 does Folder.ParseName is implimented in this way in C#? Because the documentation shows only reference/ code implementation for VB/VBScript/JS

    Please look into following thread:

    P/Invoke with Shell32, bypass Interop.Shell32.dll generation


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]



    Monday, October 2, 2017 1:06 PM
    Moderator
  • Never heard about that, and I think this would be hard to implement for me. All I ever read and seen examples for doing what I want in C# is in a simmilar way that my code in post#1. Do you have any examples of this pinvoke method ?
    Monday, October 2, 2017 1:17 PM
  • Hi LuckyLuke82,

    Thank you for posting here.

    According to your description, I add the reference of shell32.dl and then test the code.

    There is a compiler error for me.

    Could you provide more details that I could reproduce your error?

    Best Regards,

    Wendy


    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, October 3, 2017 6:57 AM
    Moderator
  • Solved. My code was wrong. 

    Folder folder_prop= shell.NameSpace(Path);

    should be

    Folder folder_prop= shell.NameSpace(dir);
    so It should be placed inside loop of EnumerateDirectories. Not sure why same code works for me in VB.NET though.



    • Edited by LuckyLuke82 Tuesday, October 3, 2017 7:08 AM
    • Marked as answer by LuckyLuke82 Tuesday, October 3, 2017 7:08 AM
    Tuesday, October 3, 2017 7:05 AM