How to get the folder of currenctly selected file

I need to get the folder of the currently selected file ( when
ProjectViewPopupMenu is clicked ).
I tried to get it from the Project , but not succeeded.

Thx


1
4 comments
Avatar
Permanently deleted user

Hello Buminda,

B> I need to get the folder of the currently selected file ( when
B> ProjectViewPopupMenu is clicked ).
B> I tried to get it from the Project , but not succeeded.

public void actionPerformed(AnActionEvent e) {
VirtualFile file = (VirtualFile) e.getDataContext().getData(DataConstants.VIRTUAL_FILE);
VirtualFile folder = file.getParent();
}

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


1

Looks like the new way to do this is:

   public void actionPerformed(AnActionEvent e)
   {
      VirtualFile file = DataKeys.VIRTUAL_FILE.getData(e.getDataContext());
      VirtualFile folder = file.getParent();
   }

1

e.getData(DataKeys.VIRTUAL_FILE) is better because it's shorter.

1
Avatar
Permanently deleted user

Hi George,

Please note that there is a possible case that more than one element is selected at the project view. You can use PlatformDataKeys.VIRTUAL_FILE_ARRAY for obtaining all of them then.

Denis

0

Please sign in to leave a comment.