6

我正在尝试创建一个插件,它会给我一个在 Eclipse 中打开的项目中所有文件的绝对路径列表。

我试过了,但我只能获得活动窗口的路径..

我的操作代码是:

  IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart(); 
    IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
    if (file == null)
        try {
            throw new FileNotFoundException();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    String path = file.getRawLocation().toOSString();
    System.out.println("path: " + path);

在这里,我只获取活动窗口的路径..但​​我想要项目中所有文件的绝对路径列表..主要是 src 文件夹下的文件...

请指导我是否可以以相同的方式进行操作,或者我是否需要为此使用一些不同的 API。

4

2 回答 2

7

经过研究,我发现下面的代码将获取 Eclipse 当前工作区项目目录的路径:

//get object which represents the workspace  
IWorkspace workspace = ResourcesPlugin.getWorkspace();  

//get location of workspace (java.io.File)  
File workspaceDirectory = workspace.getRoot().getLocation().toFile()

注意:您需要导入org.eclipse.core.resourcesorg.eclipse.core.runtime使用这些 API

来源

于 2013-03-06T12:01:46.240 回答
0

给定 an IResource,您可以使用org.eclipse.core.resources.IResource.getLocation()返回 an 的方法,IPath其中“本地文件系统中此资源的绝对路径,如果无法确定路径,则返回 null”。然后您可以使用org.eclipse.core.runtime.IPath.toOSString()返回字符串表示的方法。

于 2021-07-13T14:47:53.190 回答