列表视图 - 添加文件类型&最后修改的子项目

时间:2016-11-28 18:44:36

标签: vb.net listview

我试图添加"文件类型"和#34;最后修改"在我的Listview中添加项目时,与在资源管理器中相同,但我找不到应该为SubItem分配的属性。这是我的代码:

template<typename Type> class graph;

template<typename Type>
class edgenode {
    friend graph<Type>;
    // ...

如果有人知道该怎么做请告诉我,我希望在我的详情视图中有这个。

3 个答案:

答案 0 :(得分:1)

对于文件类型,您可以使用lvi.SubItems.Add(MyFile.Extension) 并且对于&#34;最后一次修改&#34;日期,当然最后修改了! :d lvi.SubItems.Add(MyFile.LastWriteTime.ToShortDateString)

上次写入和上次访问不一样;)

答案 1 :(得分:1)

linked answer提供了一种获取所有扩展属性的通用方法。在较新的Windows版本中有300多个元素,如果你只对一两个感兴趣,那么获取它们显然有点过分。这将返回 文件类型。更好的方法可能是传递所需属性名称的“购物清单”。

与以前一样,您需要根据您的操作系统版本添加对 Microsoft Shell控件和自动化 Microsoft Shell文件夹视图路由器的引用。

Imports Shell32
Imports SHDocVw

Partial Friend Class Shell32Methods

    Friend Shared Function GetShellFileProperty(filepath As String, index As Int32) As String
        Dim shell As New Shell32.Shell
        Dim shFolder As Shell32.Folder
        shFolder = shell.NameSpace(Path.GetDirectoryName(filepath))

        ' get shell data for this file, cast to folder item
        Dim shFolderItem = DirectCast(shFolder.Items().Item(Path.GetFileName(filepath)), 
                            Shell32.ShellFolderItem)

        If shFolderItem IsNot Nothing Then
            Return shFolder.GetDetailsOf(shFolderItem, index)
        Else
            Return String.Empty
        End If
    End Function
        ...
End Class

用法:

Dim lvi As ListViewItem
Dim fileType As String

For Each f As String In Directory.EnumerateFiles("C:\Temp\ShellTest")
    fileType = Shell32Methods.GetShellFileProperty(f, 9)

    lvi = New ListViewItem
    lvi.Text = Path.GetFileName(f)
    lvi.SubItems.Add(fileType)
    lvFiles.Items.Add(lvi)

Next

enter image description here

理想情况下,您需要为属性创建枚举,以便代码可以避免幻数:

fileType = Shell32Methods.GetShellFileProperty(f, Shell32FileProps.FileType)

如其他地方所述,> 260左右的索引可以根据OS版本而改变。这可以很容易地修改为接受一个Enum / Int数组并返回一个值列表,以防止迭代所有300+属性得到一个或三个。

答案 2 :(得分:0)

我想出了另一个解决方案,我认为这个更容易,至少对我而言:

var Color = function(options) {
  var self = this;
  self.colorName = options.colorName;
};

(function(angular) {
  'use strict';
  angular.module('myApp', []).controller('myAppController', function myAppController($filter) {
    var self = this;
    self.colorName = '';
    self.colors = [
      new Color({colorName: 'Red'}),
      new Color({colorName: 'Blue'}), 
      new Color({colorName: 'Green'})
      ];

    self.addColor = function() {
      console.log("added a new color " + self.colorName);
      self.colors.push(new Color({colorName: self.colorName}));
    };

    setTimeout(function() {
      console.log("added new color in another thread");
      self.colors.push(new Color({colorName: 'Purple'}));
        }, 1000);
    });
})(window.angular);

简单地使用Listview(这会添加文件类型子项):

 Public Function ExProperty(filepath As String, PropertyItem As Integer)
        Dim arrHeaders As New List(Of String)()

        Dim shell As New Shell
        Dim rFolder As Folder = shell.[NameSpace](Path.GetDirectoryName(filepath))
        Dim rFiles As FolderItem = rFolder.ParseName(Path.GetFileName(filepath))

        'I needed only File type so I looped to 2 only (2 is the file type in my case - Windows 10 - 
        ' to see all available properties do a loop 
        ' 0 To Short.MaxValue - 1" and then extract whatever property you like)
                    For i As Integer = 0 To 2
                        Dim value As String = rFolder.GetDetailsOf(rFiles, i).Trim()
                        arrHeaders.Add(value)
                    Next

        Dim DesiredProperty As String
        DesiredProperty = arrHeaders.Item(PropertyItem)

        Return DesiredProperty

 End Function

与所有解决方案一样,必须设置对 Microsoft Shell控件和自动化 的引用。