ArcObjects Library Reference  

ToFileGDB

About the Using list functions to migrate from personal geodatabases to file geodatabases Sample

[C#]

ToFileGDB.cs

using System;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.ConversionTools;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.esriSystem;

namespace GeodatabaseConversion
{
    class ToFileGDB
    {
        [STAThread]
        static void Main(string[] args)
        {

            if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine))
            {
                if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop))
                {
                    System.Windows.Forms.MessageBox.Show("This application could not load the correct version of ArcGIS.");
                    return;
                }
            }

            LicenseInitializer aoLicenseInitializer = new LicenseInitializer();
            if (!aoLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine, esriLicenseProductCode.esriLicenseProductCodeBasic, esriLicenseProductCode.esriLicenseProductCodeStandard, esriLicenseProductCode.esriLicenseProductCodeAdvanced },
            new esriLicenseExtensionCode[] { esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork }))
            {
                System.Windows.Forms.MessageBox.Show("This application could not initialize with the correct ArcGIS license and will shutdown. LicenseMessage: " + aoLicenseInitializer.LicenseMessage());
                aoLicenseInitializer.ShutdownApplication();
                return;
            }

            // run geoprocessing code
            ConvertPersonalGeodatabaseToFileGeodatabase();

            // shutdown application
            aoLicenseInitializer.ShutdownApplication();
            
        }

        private static void ConvertPersonalGeodatabaseToFileGeodatabase()
        {
            // Initialize the Geoprocessor
            Geoprocessor geoprocessor = new Geoprocessor();

            // Allow for the overwriting of file geodatabases, if they previously exist.
            geoprocessor.OverwriteOutput = true;

            // Set the workspace to a folder containing personal geodatabases.
            geoprocessor.SetEnvironmentValue("workspace", @"C:\data");

            // Identify personal geodatabases.
            IGpEnumList workspaces = geoprocessor.ListWorkspaces("*", "Access");
            string workspace = workspaces.Next();
            while (workspace != "")
            {
                // Set workspace to current personal geodatabase
                geoprocessor.SetEnvironmentValue("workspace", workspace);

                // Create a file geodatabase with the same name as the personal geodatabase
                string gdbname = System.IO.Path.GetFileName(workspace).Replace(".mdb", "");
                string dirname = System.IO.Path.GetDirectoryName(workspace);

                // Execute CreateFileGDB tool
                CreateFileGDB createFileGDBTool = new CreateFileGDB(dirname, gdbname + ".gdb");
                geoprocessor.Execute(createFileGDBTool, null);

                // Initialize the Copy Tool
                Copy copyTool = new Copy();

                // Identify feature classes and copy to file geodatabase
                IGpEnumList fcs = geoprocessor.ListFeatureClasses("", "", "");
                string fc = fcs.Next();
                while (fc != "")
                {
                    Console.WriteLine("Copying " + fc + " to " + gdbname + ".gdb");
                    copyTool.in_data = fc;
                    copyTool.out_data = dirname + "\\" + gdbname + ".gdb" + "\\" + fc;
                    geoprocessor.Execute(copyTool, null);
                    fc = fcs.Next();
                }

                // Identify feature datasets and copy to file geodatabase
                IGpEnumList fds = geoprocessor.ListDatasets("", "");
                string fd = fds.Next();
                while (fd != "")
                {
                    Console.WriteLine("Copying " + fd + " to " + gdbname + ".gdb");
                    copyTool.in_data = fd;
                    copyTool.data_type = "FeatureDataset";
                    copyTool.out_data = dirname + "\\" + gdbname + ".gdb" + "\\" + fd;
                    try
                    {
                        geoprocessor.Execute(copyTool, null);
                    }
                    catch (Exception ex)
                    {
                        object sev = null;
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                    fd = fds.Next();
                }

                // Identify tables and copy to file geodatabase
                IGpEnumList tbls = geoprocessor.ListTables("", "");
                string tbl = tbls.Next();
                while (tbl != "")
                {
                    Console.WriteLine("Copying " + tbl + " to " + gdbname + ".gdb");
                    copyTool.in_data = tbl;
                    copyTool.out_data = dirname + "\\" + gdbname + ".gdb" + "\\" + tbl;
                    geoprocessor.Execute(copyTool, null);
                    tbl = tbls.Next();
                }

                workspace = workspaces.Next();
            }
        }
    }
}

[Visual Basic .NET]

ToFileGDB.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Text
Imports ESRI.ArcGIS.ConversionTools
Imports ESRI.ArcGIS.DataManagementTools
Imports ESRI.ArcGIS.Geoprocessor
Imports ESRI.ArcGIS.Geoprocessing
Imports ESRI.ArcGIS.esriSystem

Namespace GeodatabaseConversion

    Friend Class ToFileGDB

        <STAThread()> _
        Shared Sub Main(ByVal args As String())


            If (Not ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine)) Then
                If (Not ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)) Then
                    System.Windows.Forms.MessageBox.Show("This application could not load the correct version of ArcGIS.")
                End If
            End If

            Dim aoLicenseInitializer As LicenseInitializer
            aoLicenseInitializer = New LicenseInitializer

            'ESRI License Initializer generated code.
            If (Not aoLicenseInitializer.InitializeApplication(New esriLicenseProductCode() {esriLicenseProductCode.esriLicenseProductCodeEngine, esriLicenseProductCode.esriLicenseProductCodeBasic, esriLicenseProductCode.esriLicenseProductCodeStandard, esriLicenseProductCode.esriLicenseProductCodeAdvanced}, _
            New esriLicenseExtensionCode() {esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork})) Then
                System.Windows.Forms.MessageBox.Show("This application could not initialize with the correct ArcGIS license and will shutdown. LicenseMessage: " + aoLicenseInitializer.LicenseMessage())
                aoLicenseInitializer.ShutdownApplication()
                Return
            End If

            ' Run geoprocessing code
            ConvertPersonalGeodatabaseToFileGeodatabase()

            ' Shutdown application
            aoLicenseInitializer.ShutdownApplication()

        End Sub



        Private Shared Sub ConvertPersonalGeodatabaseToFileGeodatabase()
            ' Initialize the Geoprocessor
            Dim geoprocessor As ESRI.ArcGIS.Geoprocessor.Geoprocessor = New ESRI.ArcGIS.Geoprocessor.Geoprocessor()

            ' Allow for the overwriting of file geodatabases, if they previously exist.
            geoprocessor.OverwriteOutput = True

            ' Set the workspace to a folder containing personal geodatabases.
            geoprocessor.SetEnvironmentValue("workspace", "C:\data")

            ' Identify personal geodatabases.
            Dim workspaces As IGpEnumList = geoprocessor.ListWorkspaces("*", "Access")
            Dim workspace As String = workspaces.Next()
            Do While workspace <> ""
                ' Set workspace to current personal geodatabase
                geoprocessor.SetEnvironmentValue("workspace", workspace)

                ' Create a file geodatabase with the same name as the personal geodatabase
                Dim gdbname As String = System.IO.Path.GetFileName(workspace).Replace(".mdb", "")
                Dim dirname As String = System.IO.Path.GetDirectoryName(workspace)

                ' Execute CreateFileGDB tool
                Dim createFileGDBTool As CreateFileGDB = New CreateFileGDB(dirname, gdbname & ".gdb")
                geoprocessor.Execute(createFileGDBTool, Nothing)

                ' Initialize the Copy Tool
                Dim copyTool As Copy = New Copy()

                ' Identify feature classes and copy to file geodatabase
                Dim fcs As IGpEnumList = geoprocessor.ListFeatureClasses("", "", "")
                Dim fc As String = fcs.Next()
                Do While fc <> ""
                    Console.WriteLine("Copying " & fc & " to " & gdbname & ".gdb")
                    copyTool.in_data = fc
                    copyTool.out_data = dirname & "\" & gdbname & ".gdb" & "\" & fc
                    geoprocessor.Execute(copyTool, Nothing)
                    fc = fcs.Next()
                Loop

                ' Identify feature datasets and copy to file geodatabase
                Dim fds As IGpEnumList = geoprocessor.ListDatasets("", "")
                Dim fd As String = fds.Next()
                Do While fd <> ""
                    Console.WriteLine("Copying " & fd & " to " & gdbname & ".gdb")
                    copyTool.in_data = fd
                    copyTool.data_type = "FeatureDataset"
                    copyTool.out_data = dirname & "\" & gdbname & ".gdb" & "\" & fd
                    Try
                        geoprocessor.Execute(copyTool, Nothing)
                    Catch ex As Exception
                        System.Windows.Forms.MessageBox.Show(ex.Message)
                    End Try

                    fd = fds.Next()
                Loop

                ' Identify tables and copy to file geodatabase
                Dim tbls As IGpEnumList = geoprocessor.ListTables("", "")
                Dim tbl As String = tbls.Next()
                Do While tbl <> ""
                    Console.WriteLine("Copying " & tbl & " to " & gdbname & ".gdb")
                    copyTool.in_data = tbl
                    copyTool.out_data = dirname & "\" & gdbname & ".gdb" & "\" & tbl
                    geoprocessor.Execute(copyTool, Nothing)
                    tbl = tbls.Next()
                Loop

                workspace = workspaces.Next()
            Loop
        End Sub

    End Class

End Namespace