從插件/RCP中取得文件路徑的方法 作者:hopeshared 引用地址:http://www./hopeshared/archive/2005/12/20/24798.html 最近社區(qū)里問這個(gè)問題的人特別多,所以在這里將自己用到的幾個(gè)方法寫出來。假如以后還有其他的方法,會(huì)進(jìn)行更新。 從插件中獲得絕對(duì)路徑: AaaaPlugin.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath()); 通過文件得到Project: IProject project = ((IFile)o).getProject(); 通過文件得到全路徑: String path = ((IFile)o).getLocation().makeAbsolute().toFile().getAbsolutePath(); 得到整個(gè)Workspace的根: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 從根來查找資源: IResource resource = root.findMember(new Path(containerName)); 從Bundle來查找資源: Bundle bundle = Platform.getBundle(pluginId); URL fullPathString = BundleUtility.find(bundle, filePath); 得到Appliaction workspace: 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(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// 得到項(xiàng)目的絕對(duì)路徑: FileLocator.toFileURL( Platform.getBundle(Application.PLUGIN_ID).getEntry("")) .getPath() /////////////////////////////////////////////////////////////////////////////////////////////////////////// 從插件中獲得絕對(duì)路徑: AaaaPlugin.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath()); getStateLocation() 報(bào)錯(cuò)NullPointerException! /////////////////////////////////////////////////////////////////////////////////////////////////////////// Location installLoc = LocationManager.getInstallLocation(); String path = null; String installPath = null; if (installLoc != null) { URL installURL = installLoc.getURL(); // assume install URL is file: based path = installURL.getPath(); } installPath = path.substring(1, path.length()); 這個(gè)方法不錯(cuò),我花了一個(gè)下午看源代碼找到的。 如果你的RCP工程沒有發(fā)布,得到的是eclipse的路徑。如果你的RCP工程已經(jīng)發(fā)布(假如在D:\RCP\下面),得到的路徑就是D:/RCP/。 大家可以試試看 。 /////////////////////////////////////////////////////////////////////////////////////////////////////////// 請(qǐng)問IEditorPart editor = ((DefaultEditDomain)(parent.getViewer().getEditDomain())).getEditorPart(); 這一句里面的parent是什么?我直接用下面的代碼取代parent.getViewer().getEditDomain()這一句可不可以? IWorkbenchPage workbenchPage = VecasCompile.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage(); IEditorPart editor = workbenchPage.getActiveEditor(); |
|