Select to view content in your preferred language

Get full path of layer (including file name and extension) in c#

4331
4
12-02-2013 09:01 AM
VeraGreen
Emerging Contributor
Hi,
I have been struggling for a long time now trying to replicate the functionality of the python code: lyr.dataSource where lyr is a layer in the map.
This returns something like:
\\Data\lakes.sdc\lakes for a GD
\\Data\lakes.shp for a shapefile
\\Data\lakes.jpg for a image
etc.

I have followed this example: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//004900000092000000
However the following code:
Return (dataset.Workspace.PathName & "\" & dataset.Name)
Does NOT return what I need.  The .pathName returns the path which is fine but the .Name is the Layers DISPLAY name (what it is called in the map.  It is NOT the name of the source file and does not include the file extension.

So what object and property will return the source file name?

Thanks,
Vera
0 Kudos
4 Replies
SanderSchaminee
Emerging Contributor
Are you sure you didn't try IDataset.BrowseName instead of .Name?
And you did include the extra "\" in between, right? This is important...
Otherwise, try IDataset.FullName.NameString.
0 Kudos
VeraGreen
Emerging Contributor
thank you for your reply - much appreciated. 

My first error was that I was not using the Feature Class. 
ILayer2 pLayer;
//Inside standard layer loop:
pFLayer = pLayer as IFeatureLayer2;
IDataset dataset = pFLayer.FeatureClass as IDataset;

dataset.FullName.NameString;  returns nothing (null)
dataset.Workspace.PathName; returns the folder path
dataset.Name; does now that I am using the FeatureClass return the file name with NO extension.
dataset.BrowseName; returns the same as dataset.Name.

then I am using:
currentLExtention = pFLayer.DataSourceType.ToString();
if (currentLExtention.Contains("Shapefile"))
     currentLExtention = ".shp";
else
     currentLExtention = "";

to get the shape file extension.

Interestingly for a raster data-set all I need is:
IRasterLayer pRLayer = pLayer as IRasterLayer;
currentLName = pRLayer.FilePath;//provides the file path AND the file name WITH the extension.  Too bad it can't be this simple for a feature layer.
0 Kudos
SanderSchaminee
Emerging Contributor
Yes, that's a similar approach to what has been described in this thread:
http://forums.arcgis.com/threads/4370-How-do-I-get-the-file-path-of-a-shapefile-or-layer-loaded-in-m...
0 Kudos
VeraGreen
Emerging Contributor
Yes - I used that post.  But still I wish there was an easier way to accomplish this . . .  seams like basic functionality.
0 Kudos