IWorkspaceFactory的Create()函數失敗原因

在使用IWorkspaceFactory接口創建一個mdb文件的時候嗎,發現報錯,而且沒找到原因。代碼如下

 string dstDir = System.IO.Path.GetDirectoryName(dstfile);
                string dstName = System.IO.Path.GetFileName(dstfile);
                if (Directory.Exists(dstfile))
                {
                    Directory.Delete(dstfile, true);
                }

                wf2.Create(dstDir, dstName, null, 0);
                dstWs = wf2.OpenFromFile(dstfile, 0) as IFeatureWorkspace;

                TransGDB(srcWs, dstWs);

在 wf2.Create處報錯,報錯沒有寫任何提示。

自己找了一下原因,發現是因爲dstDir對應的路徑不存在,而接口沒有做這個沒有路徑的處理,因此會報錯,改代碼爲:

 string dstDir = System.IO.Path.GetDirectoryName(dstfile);
                string dstName = System.IO.Path.GetFileName(dstfile);
                ///////////////////////////////
               if (!Directory.Exists(dstDir))
                {
                    Directory.CreateDirectory(dstDir);
                }
                ////////////////////////////////
                if (Directory.Exists(dstfile))
                {
                    Directory.Delete(dstfile, true);
                }

                wf2.Create(dstDir, dstName, null, 0);
                dstWs = wf2.OpenFromFile(dstfile, 0) as IFeatureWorkspace;

                TransGDB(srcWs, dstWs);

既可以運行了

發佈了71 篇原創文章 · 獲贊 39 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章