Files
ShrlAlgoToolkit/ShrlAlgoStudio/RevitLess/StorageBase.cs

163 lines
3.2 KiB
C#
Raw Normal View History

2025-07-11 09:20:23 +08:00

using System;
using System.IO;
using System.IO.Packaging;
using System.Reflection;
2026-02-12 21:29:00 +08:00
namespace ShrlAlgoStudio.RevitLess
2025-07-11 09:20:23 +08:00
{
public abstract class StorageBase
{
#region Private Variables
2026-01-02 16:37:37 +08:00
private string fileName = string.Empty;
private bool isInitialized = false;
2025-07-11 09:20:23 +08:00
#endregion
#region Constructors
public StorageBase(string fileName)
{
if(File.Exists(fileName) == false)
{
throw new FileNotFoundException(string.Format("The file \"{0}\" was not found.", fileName));
}
FileName = fileName;
}
#endregion
#region Public Properties
public bool IsInitialized
{
get
{
2026-01-02 16:37:37 +08:00
return isInitialized;
2025-07-11 09:20:23 +08:00
}
protected set
{
2026-01-02 16:37:37 +08:00
isInitialized = value;
2025-07-11 09:20:23 +08:00
}
}
public string FileName
{
get
{
2026-01-02 16:37:37 +08:00
return fileName;
2025-07-11 09:20:23 +08:00
}
protected set
{
2026-01-02 16:37:37 +08:00
fileName = value;
2025-07-11 09:20:23 +08:00
}
}
public string FileExtension
{
get
{
if(string.IsNullOrEmpty(FileName))
{
return string.Empty;
}
return Path.GetExtension(FileName).Replace(".", string.Empty);
}
}
public DocumentType DocType
{
get
{
if(string.IsNullOrEmpty(FileExtension))
{
return DocumentType.Unknown;
}
switch(FileExtension.ToUpper())
{
case "RVT":
return DocumentType.Project;
case "RTE":
return DocumentType.ProjectTemplate;
case "RFA":
return DocumentType.Family;
case "RFT":
return DocumentType.FamilyTemplate;
default:
return DocumentType.Unknown;
}
}
}
#endregion
#region Private Properties
#endregion
#region Public Methods
#endregion
#region Private Methods
#endregion
#region Protected Methods
protected object InvokeStorageRootMethod(StorageInfo storageRoot,
string methodName,
params object[] methodArgs)
{
//We need the StorageRoot class to directly open an OSS file. Unfortunately, it's internal.
//So we'll have to use Reflection to access it. This code was inspired by:
//http://henbo.spaces.live.com/blog/cns!2E073207A544E12!200.entry
//Note: In early WinFX CTPs the StorageRoot class was public because it was documented
//here: http://msdn2.microsoft.com/en-us/library/aa480157.aspx
BindingFlags bindingFlags = BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.InvokeMethod;
Type storageRootType = typeof(StorageInfo).Assembly.GetType("System.IO.Packaging.StorageRoot",
true,
false);
//try
//{
object result = storageRootType.InvokeMember(methodName,
bindingFlags,
null,
storageRoot,
methodArgs);
return result;
//}
//catch (Exception ex)
//{
// throw;
//}
////return null;
}
#endregion
}
}