can't openworkspace

2845
4
09-07-2010 01:52 AM
Highlighted
New Contributor
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.NetworkAnalyst;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            IFeatureWorkspace featureWorkspace;


            // Open GDB and NDS
            featureWorkspace = OpenWorkspace(Application.StartupPath + @"\..\..\..\..\..\Data\SanFranciscoNetwork") as IFeatureWorkspace;
           

        }

            public IWorkspace OpenWorkspace(string strGDBName)
        {
            IWorkspaceFactory workspaceFactory;
            workspaceFactory = new ShapefileWorkspaceFactory();
            return workspaceFactory.OpenFromFile(strGDBName, 0);
        }



        }

    }
above is my code,there is error:unexpected COMException,error from HRESULT:0x80040258 when running at line "return workspaceFactory.OpenFromFile(strGDBName, 0);"
please help me
thank you!
shirly
Tags (2)
Reply
0 Kudos
4 Replies
Highlighted
New Contributor III
First, make sure your encoded file path is 100% correct (i.e., that the path you are specifying actually exists; you can check this through the debugger, if necessary, since it is based partly on the StartupPath). Second, make sure that you pass the path to the folder containing the shapefile, and do not include the name of the shapefile itself. For example, if you have a Streets.shp file in your C:\TEMP\ folder, you should pass only the path C:\TEMP\ and not C:\TEMP\Streets.
Reply
0 Kudos
Highlighted
New Contributor
First, I am sure my encoded file path is  correct (the data are examples of Arcgis).Second,the path to the folder contains the shapefile.(i.e.,C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\SanFranciscoNetwork\Streets.shp).According to the example of Arcgis--C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\Engine\ClosestFacilitySolver,I can run the example,but when I copy the code to my project with the error(can't openworkspace),is that my software's problem? I use AE9.3+VS2005.


Thank you very much!
shirly
Reply
0 Kudos
Highlighted
New Contributor
Hi Alan,


Thank you very much for your help ,I can open my workspace using the absolute path.Now I have anthor problem,below is my code.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.NetworkAnalyst;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesGDB;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
private INAContext m_NAContext;
public Form1()
{
InitializeComponent();
Initialize();
}


private void Initialize()
{
IFeatureWorkspace featureWorkspace;
INetworkDataset networkDataset;

// Open GDB and NDS

//featureWorkspace = OpenWorkspace(@"C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\SanFranciscoNetwork") as IFeatureWorkspace;
  featureWorkspace = OpenWorkspace(@"E:\wlmq\test.mdb") as IFeatureWorkspace;

networkDataset = OpenNetworkDataset(featureWorkspace as IWorkspace, "test_ND");
//networkDataset = OpenNetworkDataset(featureWorkspace as IWorkspace, "Streets_nd");


//Create Layer for Network Dataset and add to ArcMap
ILayer layer;
INetworkLayer networkLayer;
networkLayer = new NetworkLayerClass();
networkLayer.NetworkDataset = networkDataset;
layer = networkLayer as ILayer;
layer.Name = "Network Dataset";
axMapControl1.AddLayer(layer, 0);


}

public IWorkspace OpenWorkspace(string strGDBName)
{
IWorkspaceFactory workspaceFactory;
//workspaceFactory = new ShapefileWorkspaceFactory();
workspaceFactory = new AccessWorkspaceFactory();
return workspaceFactory.OpenFromFile(strGDBName, 0);
}

public INetworkDataset OpenNetworkDataset(IWorkspace workspace, string strNDSName)
{
IWorkspaceExtensionManager workspaceExtensionManager;
IWorkspaceExtension workspaceExtension;
IDatasetContainer2 datasetContainer2;

// Get Workspace Extension
workspaceExtensionManager = workspace as IWorkspaceExtensionManager;
int count = workspaceExtensionManager.ExtensionCount;
for (int i = 0; i < count; i++)
{
workspaceExtension = workspaceExtensionManager.get_Extension(i);
if (workspaceExtension.Name.Equals("Network Dataset"))
{
datasetContainer2 = workspaceExtension as IDatasetContainer2;
try
{
return datasetContainer2.get_DatasetByName(esriDatasetType.esriDTNetworkDataset, strNDSName) as INetworkDataset;

}
catch
{

}
}
}
return null;
}



}

}


Now , I can run my project and no error.But I canâ??t see my networkDataset's geography in axMapControl1.So I try to change my workspace path and my networkDataset to "featureWorkspace = OpenWorkspace(@"C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\SanFranciscoNetwork") as IFeatureWorkspace;" and
"networkDataset = OpenNetworkDataset(featureWorkspace as IWorkspace, "Streets_nd");",the result display the geography.Why can't my networkDataset diaplay normally?


Please tell me why ,Thank you!


Shirly
Reply
0 Kudos
Highlighted
New Contributor
Hi Alan,
Thank you very much for your help! Now I can run my project normally,Thanks again!!



Shirly
Reply
0 Kudos