//按下新建文件GDB按钮 private void button1_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "文件地理数据库(*.gdb)|*.gdb"; saveFileDialog.Title = "新建文件地理数据库"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { if (System.IO.File.Exists(saveFileDialog.FileName)) { MessageBox.Show("文件已存在!"); return; } try { int index = saveFileDialog.FileName.LastIndexOf("\\"); string GDBName = saveFileDialog.FileName.Substring(index + 1); string GDBPath = saveFileDialog.FileName.Substring(0, index); IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactory(); pWorkspaceFactory.Create(GDBPath, GDBName, null, 0); MessageBox.Show("创建成功!", "提示"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
public static IWorkspace CreateFileGDB(string string_0) { IWorkspace workspace = null; IWorkspaceFactory fileGDBWorkspaceFactoryClass = new FileGDBWorkspaceFactory(); try { IWorkspaceName workspaceName = fileGDBWorkspaceFactoryClass.Create(System.IO.Path.GetDirectoryName(string_0), System.IO.Path.GetFileNameWithoutExtension(string_0), null, 0); workspace = (workspaceName as IName).Open() as IWorkspace; } catch (COMException cOMException1) { COMException cOMException = cOMException1; int errorCode = cOMException.ErrorCode; MessageBox.Show(string.Concat("错误代码:", errorCode.ToString(), "\r\n", cOMException.Message)); } catch (Exception exception) { } ComReleaser.ReleaseCOMObject(fileGDBWorkspaceFactoryClass); fileGDBWorkspaceFactoryClass = null; return(workspace); }
public override void OnClick() { IGxObject firstObject = ((IGxSelection)_context.GxSelection).FirstObject; if (firstObject is IGxFile) { string path = (firstObject as IGxFile).Path; path = (path[path.Length - 1] != '\\' ? string.Concat(path, "\\新建文件型数据库") : string.Concat(path, "新建文件型数据库")); string str = string.Concat(path, ".gdb"); int num = 1; while (Directory.Exists(str)) { num++; str = string.Concat(path, " (", num.ToString(), ").gdb"); } IWorkspaceFactory fileGDBWorkspaceFactoryClass = new FileGDBWorkspaceFactory(); try { IWorkspaceName workspaceName = fileGDBWorkspaceFactoryClass.Create(Path.GetDirectoryName(str), Path.GetFileNameWithoutExtension(str), null, 0); IGxObject gxDatabase = new GxDatabase(); (gxDatabase as IGxDatabase).WorkspaceName = workspaceName; IGxCatalog catalog = GxCatalogCommon.GetCatalog(firstObject); gxDatabase.Attach(firstObject, catalog); catalog.ObjectAdded(gxDatabase); } catch (Exception exception) { MessageBox.Show(exception.Message); } } }
private void CreaWorkSpaceOutput(out IWorkspaceName WorkspaceName, out string pathFGDB) { IWorkspaceFactory2 workF = new FileGDBWorkspaceFactory() as IWorkspaceFactory2; Guid g = Guid.NewGuid(); string timeStamp = DateTime.UtcNow.ToString("dd-MM-yyyy", CultureInfo.CurrentUICulture); string basePath = $@"C:\arcgisserver\directories\arcgisoutput\_ags_{timeStamp}"; string nomeFGDB = $@"FGDB_{g}.gdb"; pathFGDB = System.IO.Path.Combine(basePath, nomeFGDB); if (!System.IO.Directory.Exists(basePath)) { System.IO.Directory.CreateDirectory(basePath); } #region Parte dedicata alla creazione di un File Geodatabase // Istanzio l'oggetto singleton IWorkspaceFactory2 workspaceFactory = new FileGDBWorkspaceFactory() as IWorkspaceFactory2; WorkspaceName = workspaceFactory.Create(basePath, nomeFGDB, null, 0); #endregion #region Rilascio tutti gli oggetti singleton int resLeft = 0; do { resLeft = System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFactory); }while (resLeft > 0); #endregion }
//创建GDB public IWorkspace CreateGDB(string gdbPath, string gdbName) { IPropertySet ps = new PropertySetClass(); ps.SetProperty("DATABASE", gdbPath); IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory(); IWorkspaceName newWorkspacename = workspaceFactory.Create(gdbPath, gdbName, ps, 0); IName name = (IName)newWorkspacename; IWorkspace newWorkspace = (IWorkspace)name.Open(); return(newWorkspace); }
/// <summary> /// Creates an fgdb in the given path. /// </summary> /// <param name="fgdbPath"></param> public static void CreateFgdbWorkspace(string fgdbPath) { if (string.IsNullOrWhiteSpace(fgdbPath)) { return; } var parentDir = Directory.GetParent(fgdbPath).FullName; string fgdbName = Path.GetFileName(fgdbPath); IWorkspaceFactory scratchWorkspaceFactory = new FileGDBWorkspaceFactory(); IWorkspaceName workspaceName = scratchWorkspaceFactory.Create(parentDir, fgdbName, null, 0); Marshal.FinalReleaseComObject(workspaceName); }
private static string GetDefaultWorkspace() { string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string tempDir = System.IO.Path.Combine(documents, "temp"); string workspacePath = System.IO.Path.Combine(tempDir, "Scratch.gdb"); IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory(); if (!workspaceFactory.IsWorkspace(workspacePath)) { IWorkspaceName defaultWorkspaceName = workspaceFactory.Create(tempDir, "Scratch", null, 0); return(defaultWorkspaceName.PathName); } else { return(workspacePath); } }
public static IWorkspace CreateFileGDB(string string_0) { IWorkspace workspace = null; IWorkspaceFactory o = new FileGDBWorkspaceFactory(); try { workspace = (o.Create(System.IO.Path.GetDirectoryName(string_0), System.IO.Path.GetFileNameWithoutExtension(string_0), null, 0) as IName).Open() as IWorkspace; } catch (COMException exception) { MessageBox.Show("错误代码:" + exception.ErrorCode.ToString() + "\r\n" + exception.Message); } catch (Exception) { } ComReleaser.ReleaseCOMObject(o); o = null; return(workspace); }