简体   繁体   English

获取/设置TShellListView路径/文件夹为字符串(不使用.Root)

[英]Get/Set TShellListView Path/Folder as String (Not Using .Root)

I want to set the path for a TShellListView to display a directory of files using Delphi 2007. I can initially use TShellListView.Root to set the root path like this and it shows the directory I want: 我想设置一个TShellListView的路径,以使用Delphi 2007显示文件目录。我最初可以使用TShellListView.Root设置这样的根路径,它显示我想要的目录:

View := TShellListView.Create(Self);
// ...
View.Root := 'C:\Windows';

But if the user navigates away from that directory using backspace and I try to set the .Root back to the original directory, the directory displayed does not change. 但是,如果用户使用退格键离开该目录,而我尝试将.Root设置回原始目录,则显示的目录不会更改。 It looks like .Root is meant to define the root of the shell namespace, not the current directory. 看起来.Root是用来定义Shell名称空间的根目录,而不是当前目录。

Also, if the user navigates around (using backspace, etc.) the .Root property does not update to reflect the currently displayed path. 同样,如果用户浏览(使用退格键等),. Root属性不会更新以反映当前显示的路径。 There is no .Path property like there is for TShellTreeView. 没有像TShellTreeView那样的.Path属性。

What I want is a way to get and set the current path as a string without being required to link the TShellListView to a TShellTreeView and set TShellTreeView.Path or hack ShellCtrls.pas since the relevant methods of TShellListView all look private. 我想要的是一种获取当前路径并将其设置为字符串的方法,而无需将TShellListView链接到TShellTreeView并设置TShellTreeView.Path或hack ShellCtrls.pas,因为TShellListView的相关方法都是私有的。 I find it hard to believe there isn't a straightforward way to get/set the path, so I assume I'm missing something simple here, but this component is not documented at all. 我发现很难相信没有简单的方法来获取/设置路径,因此我认为这里缺少一些简单的东西,但是此组件根本没有记录。

You can get the currently loaded path using 您可以使用获取当前加载的路径

ShellListView1.RootFolder.PathName

Setting the Root property works, but it isn't updated when you change folders interactively. 设置“根”属性可以使用,但是当您以交互方式更改文件夹时,不会更新该属性。 So you need to force it to think there's a change. 因此,您需要强迫它认为有变化。 This works if you're always resetting it to the same original path: 如果您始终将其重置为相同的原始路径,则此方法有效:

ShellListView1.Root := View.RootFolder.PathName; // Updates to current location
ShellListView1.Root := 'C:\Windows';

Alternatively, for arbitrary paths you could just add/remove the trailing \\ in order to fool the SameText check in SetRoot: 另外,对于任意路径,您可以仅添加/删除尾随\\以欺骗SetRoot中的SameText检查:

if ShellListView1.Root[Length(ShellListView1.Root)] = '\' then
  ShellListView1.Root := ExcludeTrailingPathDelimiter(ANewPath)
else
  ShellListView1.Root := IncludeTrailingPathDelimiter(ANewPath);

To get the current folder as a string, you can access the RootFolder-property. 要以字符串形式获取当前文件夹,可以访问RootFolder属性。

procedure TForm2.Button1Click(Sender: TObject);
begin
  showmessage(ShellListView1.RootFolder.PathName);
end;

To set the current folder as a string, you use the root-property. 要将当前文件夹设置为字符串,请使用root属性。

procedure TForm2.Button2Click(Sender: TObject);
begin
  ShellListView1.Root := 'C:\windows';
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM