ConvertNodeXLWorkbook
(
String otherWorkbookFile,
String convertedWorkbookFile
)
{
Debug.Assert( !String.IsNullOrEmpty(otherWorkbookFile) );
Debug.Assert( File.Exists(otherWorkbookFile) );
Debug.Assert( !String.IsNullOrEmpty(convertedWorkbookFile) );
// The application's template is needed to get the customization
// information.
String sTemplatePath;
if ( !ApplicationUtil.TryGetTemplatePath(out sTemplatePath) )
{
throw new NodeXLWorkbookConversionException(
ApplicationUtil.GetMissingTemplateMessage() );
}
try
{
File.Copy(otherWorkbookFile, convertedWorkbookFile, true);
}
catch (UnauthorizedAccessException)
{
throw new NodeXLWorkbookConversionException(
"The converted copy already exists and is read-only. It can't"
+ " be overwritten."
);
}
catch (IOException oIOException)
{
if ( oIOException.Message.Contains(
"it is being used by another process") )
{
throw new NodeXLWorkbookConversionException(
"The converted copy already exists and is open in Excel."
+ " It can't be overwritten."
);
}
throw (oIOException);
}
// Remove the other customization.
try
{
if (ServerDocument.GetCustomizationVersion(
convertedWorkbookFile) > 0)
{
ServerDocument.RemoveCustomization(convertedWorkbookFile);
}
}
catch (Microsoft.VisualStudio.Tools.Applications.Runtime.
UnknownCustomizationFileException)
{
throw new NodeXLWorkbookConversionException(
"The file doesn't appear to be an Excel workbook."
);
}
// Create a ServerDocument from the application's template. The
// solution ID and deployment manifest name will be obtained from this.
using ( ServerDocument oTemplateServerDocument =
new ServerDocument(sTemplatePath, FileAccess.Read) )
{
// For some reason, ServerDocument.AddCustomization() also requires
// a path to the NodeXL assembly file, even though it doesn't get
// embedded in the document.
String sAssemblyFile = new Uri(
Assembly.GetExecutingAssembly().CodeBase).LocalPath;
String [] asNonPublicCachedDataMembers;
ServerDocument.AddCustomization(convertedWorkbookFile,
sAssemblyFile, oTemplateServerDocument.SolutionId,
oTemplateServerDocument.DeploymentManifestUrl, false,
out asNonPublicCachedDataMembers);
}
}