2008-11-18 9 views
14

में वर्तमान में संपादित फ़ाइल का पूर्ण पथ प्राप्त करें मैं एक प्लगइन लिखना चाहता हूं जो एक्लिप्स में वर्तमान में संपादित फ़ाइल के साथ कुछ करता है। लेकिन मुझे यकीन नहीं है कि फ़ाइल के पूर्ण पथ को सही तरीके से कैसे प्राप्त किया जाए।ईक्लीप्स

यह अब मुझे क्या करना है:

IFile file = (IFile) window.getActivePage().getActiveEditor.getEditorInput(). 
    getAdapter(IFile.class); 

अब मैं एक iFile वस्तु है, और मैं प्राप्त कर सकते हैं यह पथ यहां है:

file.getFullPath().toOSString(); 

हालांकि यह अभी भी सिर्फ मेरे पथ रिश्तेदार के लिए देता है कार्यक्षेत्र। मैं उससे पूर्ण पथ कैसे प्राप्त कर सकता हूं?

उत्तर

20

ऐसा लगता है कि आप IResource.getRawLocation() चाहते हैं। यह IPath देता है, जिसमें makeAbsolute() विधि भी होती है यदि आप दोगुना होना चाहते हैं कि आपके पास एक पूर्ण पथ है।

+0

IResource.getRawLocation() से लिंक अब बदल दिया गया है। –

+0

धन्यवाद विकास - लिंक को अपडेट किया गया –

1

मैं आमतौर पर IFile.getLocation() को कॉल करता हूं जो आईपैथ देता है और फिर IPath.toOSString() को कॉल करता है।

file.getLocation().toOSString() 
0
IWorkspace ws  = ResourcesPlugin.getWorkspace(); 
IProject project = ws.getRoot().getProject("*project_name*"); 

IPath location = new Path(editor.getTitleToolTip()); 
IFile file  = project.getFile(location.lastSegment()); 

into file.getLocationURI() it's the absolute path 
5

मुझे लगता है कि एक और अधिक जावा अनुकूल समाधान निम्न का उपयोग करने के लिए होगा:

IResource.getLocation().toFile() 

यह IPath एपीआई (getLocation() हिस्सा) का लाभ लेता है और एक वापस आ जाएगी java.io.File उदाहरण। बेशक अन्य उत्तरों शायद आपको उस स्थान पर ले जाएंगे जहां आप भी बनना चाहते हैं।

एक स्पर्शिक नोट पर, मुझे संपादकों की बात आती है जब मुझे आईडीई कक्षा (org.eclipse.ui.ide.IDE) उपयोगी उपयोगिता संसाधन मिल जाता है।

4

जवाब यह है कि (! और मैं इसे परीक्षण किया) मेरे लिए काम किया था:

// Get the currently selected file from the editor 
IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart(); 
IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class); 
if (file == null) throw new FileNotFoundException(); 
String path = file.getRawLocation().toOSString(); 
System.out.println("path: " + path); 
-2

मेरे लिए, यह रन ठीक।

IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace()। GetRoot();

फ़ाइल फ़ाइल = workSpaceRoot.getRawLocation()। MakeAbsolute()। ToFile(); इस स्थान से

फ़ाइल सूची:

फ़ाइल [] फ़ाइलें = file.listFiles();

संबंधित मुद्दे