不开启Revit查看revit文件版本
2024年8月14日大约 1 分钟
代码
public static class RevitFileUtils
{
/// <summary>
/// 获取revit文件版本号[采用流方式]返回结果(eg:2018,2019),支持2016-2021
/// </summary>
/// <param name="filePath"></param>
/// <returns>返回结果(eg:2018,2019)</returns>
public static string GetVersion(string filePath)
{
const string MatchVersion = @"((?<=Autodesk Revit )20\d{2})|((?<=Format: )20\d{2})";
var version = string.Empty;
Encoding useEncoding = Encoding.Unicode;
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
//匹配字符有20个(最长的匹配字符串18版本的有20个),为了防止分割对匹配造成的影响,需要验证20次偏移结果
for (int i = 0; i < 20; i++)
{
byte[] buffer = new byte[2000];
file.Seek(i, SeekOrigin.Begin);
while (file.Read(buffer, 0, buffer.Length) != 0)
{
var head = useEncoding.GetString(buffer);
Regex regex = new Regex(MatchVersion);
var match = regex.Match(head);
if (match.Success)
{
version = match.ToString();
return version;
}
}
}
}
return version;
}
/// <summary>
/// 获取Revit文件的版本,支持2016-2018
/// </summary>
/// <param name="path">文件路径</param>
/// <returns></returns>
public static string GetRevitVision(string path)
{
string revitVision = null;
FileStream stream = new FileStream(path, FileMode.Open);
int size = 1024 * 1024;
byte[] bytes = new byte[size];
while (stream.Read(bytes, 0, size) > 0)
{
string str = Encoding.Unicode.GetString(bytes);
string pattern = @"Autodesk Revit \d{4}";
var match = Regex.Match(str, pattern);
if (match.Success)
{
revitVision = match.Value.Substring(match.Length - 4, 4);
break;
}
}
return revitVision;
}
}
测试数据
- 百度网盘:链接: https://pan.baidu.com/s/1ar7jIvHqBHev0k4aYvmQaQ?pwd=yph5 提取码: yph5