我正在自动化使用 CSV 文件的邮件合并过程。此过程的一部分需要创建一个数据库(OpenOffice odb 文件),然后将其注册为数据源。当我来删除数据库时,我收到一个异常,指出“无法删除您的文件:它正在被其他人或程序使用”。问题是我无法让 OpenOffice 进程释放此资源(不杀死它)。我目前的代码是:
public string DeleteDatasource(string datasourceName)
{
string result = string.Empty;
object databaseContext = _MultiServiceFactory.createInstance("com.sun.star.sdb.DatabaseContext");;
try
{
XDatabaseRegistrations databaseRegistrations = (XDatabaseRegistrations)databaseContext;
if(databaseRegistrations.hasRegisteredDatabase(datasourceName))
{
/* //attempt one
XNameAccess nameAccess = (XNameAccess)OODatabaseContext;
object datasource = nameAccess.getByName(datasourceName);
XNamingService namingservice = (XNamingService)OODatabaseContext;
namingservice.revokeObject(datasourceName);
*/
//attempt 2
string databaseLocation = databaseRegistrations.getDatabaseLocation(datasourceName);
databaseRegistrations.revokeDatabaseLocation(datasourceName);
if (!String.IsNullOrEmpty(databaseLocation))
try
{
//As File Path converts the uno file string into a standard form i.e. "file:///c:/temp/DatabaseFile.odb" to "c:\\temp\\DatabaseFile.odb"
File.Delete(databaseLocation.AsFilepath());
}
catch (System.Exception ex)
{
//some error handling
}
}
return result;
}
catch (System.Exception ex)
{
//More error handling
}
}
任何想法如何取消注册此数据源,以便我可以删除 odb。谢谢