maphew

Set default GDB from arcpy?

how to set gdb as my default gdb by arcpy, without opening ArcGIS desktop? Setting env workspace does not seem to work here:

arcpy.CreateFileGDB_management('e:/Rstack', 'new.gdb') 
arcpy.env.workspace = 'e:/Rstack/new.gdb' 

From < https://gis.stackexchange.com/questions/197926/setting-default-gdb-from-arcpy

This took a bit of digging, but it annoyed me enough this morning to dig almost all the way down. Unfortunately I didn't find an accessible solution for v10 arcpy, however in Pro it's available out of the box as arcpy.mp.ArcGISProject().defaultGeodatabase

python ArcGIS Pro 

import arcpy 
aprx_path = r'X:\Projects\2019\ENV.533.aprx' 
gdb_path = r'X:\shared\ENV_default.gdb' 
aprx = arcpy.mp.ArcGISProject(aprx_path) # or 'CURRENT' in live py console 
aprx.defaultGeodatabase = gdb_path 
del arcpy.env.workspace # Optional, ensure default gdb used in rest of this script 

From https://pro.arcgis.com/en/pro-app/arcpy/mapping/arcgisproject-class.htm 

python ArcMap 10 

# Not real code! waiting/digging for solution 
import arcpy.mapping.convertArcObjectToPythonObject as convert 
defgdb = convert('IGxCatalogDefaultDatabase') 
defgdb.DefaultDatabaseName = r"D:\path\to\default.gdb" 

From < http://your.kudos.link/here

C# ArcGIS Pro 

//Create an empty project. The project will be created in the default folder 
//It will be named MyProject1, MyProject2, or similar... 
await Project.CreateAsync(); 
//Gets the current project 
var project = Project.Current; 
//Get default gdb path 
var projGDBPath = Project.Current.DefaultGeodatabasePath; 

From https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-content#get-the-projects-default-gdb-path 

C# ArcMap 10 

IWorkspaceName name = new WorkspaceNameClass(); 
name.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory"; 
name.PathName = @"E:\Projects\WorldSoil\ProjectData.gdb"; 
IGxCatalogDefaultDatabase gdb = app as IGxCatalogDefaultDatabase; 
gdb.DefaultDatabaseName = name; 

From http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/IGxCatalogDefaultDatabase_Interface/0014000000qw000000/ 

VBA ArcMap 10 

Private Function MxDocument_OpenDocument() As Boolean 
Dim pWorkspaceName As IWorkspaceName 
Set pWorkspaceName = New WorkspaceName 
pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory" 
pWorkspaceName.PathName = "C:\Scratch\MyDefaultGDB.gdb" 
Dim pGxCatalogDefaultDatabase As IGxCatalogDefaultDatabase 
Set pGxCatalogDefaultDatabase = Application 
pGxCatalogDefaultDatabase.DefaultDatabaseName = pWorkspaceName 
End Function 

Placing this code into the Normal template section means that the code will be executed every time an MXD is opened.

From https://gis.stackexchange.com/a/192717/108 

From < https://gis.stackexchange.com/questions/197926/setting-default-gdb-from-arcpy?noredirect=1&lq=1