在使用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);
既可以運行了