最近
社区里问这个问题的人特别多,所以在这里将自己用到的几个方法写出来。假如以后还有其他的方法,会进行更新。
从插件中获得绝对路径:
AaaaPlugin.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath());
IProject project
=
((IFile)o).getProject();
String path
=
((IFile)o).getLocation().makeAbsolute().toFile().getAbsolutePath();
IWorkspaceRoot root
=
ResourcesPlugin.getWorkspace().getRoot();
IResource resource
=
root.findMember(
new
Path(containerName));
Bundle bundle
=
Platform.getBundle(pluginId);
URL fullPathString
=
BundleUtility.find(bundle, filePath);
Platform.asLocalURL(PRODUCT_BUNDLE.getEntry(
""
)).getPath()).getAbsolutePath();
得到runtimeworkspace:
Platform.getInstanceLocation().getURL().getPath();
从编辑器来获得编辑文件
IEditorPart editor
=
((DefaultEditDomain)(parent.getViewer().getEditDomain())).getEditorPart();
IEditorInput input
=
editor.getEditorInput();
if
(input
instanceof
IFileEditorInput)
{
IFile file = ((IFileEditorInput)input).getFile();
}
从插件中获得绝对路径:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
通过文件得到Project:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
通过文件得到全路径:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
得到整个Workspace的根:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
从根来查找资源:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
从Bundle来查找资源:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
得到Appliaction workspace:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
得到runtimeworkspace:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
从编辑器来获得编辑文件
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/8f1ba5b45633e9678d1db480c16cae3f.gif)