简体   繁体   English

使用C#获取SP配置数据库版本

[英]Get SP Configuration Database Version in C#

I'm looking to grab the SharePoint Configuration Database version via C# and found the SPConfigDatabase class under Microsoft.SharePoint.Administration which has a Version member. 我希望通过C#获取SharePoint配置数据库版本,并在Microsoft.SharePoint.Administration下找到具有Version成员的SPConfigDatabase类 This is exactly what I'm looking for, but this is now obsolete. 这正是我正在寻找的,但现在已经过时了。

What is the best method for doing this? 这样做的最佳方法是什么? Ideally I'd like to grab the Cumulative Update date as well, though this is essentially just another name for the version number. 理想情况下,我也想抓住累积更新日期,尽管这实际上只是版本号的另一个名称。

Thanks 谢谢

The way to get it using C# is to use the Microsoft.SharePoint.Administration.SPFarm class and the property you are looking for is BuildVersion : 使用C#获取它的方法是使用Microsoft.SharePoint.Administration.SPFarm类,您要查找的属性是BuildVersion

var v = SPFarm.Local.BuildVersion;

As that returns a System.Version object, you may want to concatenate the values together to make the string appear as it does in the central admin: 当它返回一个System.Version对象时,您可能希望将值连接在一起以使字符串像在中央管理中一样显示:

var friendly = v.Major + "." + v.Minor + "." + v.Build + "." + v.Revision;

Also, please note that to access the SPFarm.Local property, you will need to be running the code in 64-bit mode as well as have an authorized account. 另请注意,要访问SPFarm.Local属性,您需要以64位模式运行代码并拥有授权帐户。 In fact, with the UAC enabled on my VM I needed to run my code by right-clicking on it and choosing "Run as Administrator" as well. 实际上,在我的VM上启用UAC后,我需要通过右键单击它并选择“以管理员身份运行”来运行我的代码。

I found a tentative solution by executing a powershell script. 我通过执行powershell脚本找到了一个暂定的解决方案。 Here's my solution that prints out the result to the console from the powershell script: 这是我的解决方案,它从powershell脚本将结果打印到控制台:

using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
.
.
.
MAIN CODE:
Runspace runspace = RunspaceFactory.CreateRunspace();

runspace.Open();


PowerShell ps = PowerShell.Create();
ps.AddScript("Add-PsSnapin Microsoft.SharePoint.Powershell");
ps.AddScript("get-spfarm | select BuildVersion");
ps.AddCommand("Out-String");

Collection <PSObject> results = ps.Invoke();
runspace.Close();

foreach (PSObject obj in results)
{
    Console.WriteLine(obj.ToString());
}

Credit to http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C and http://social.msdn.microsoft.com/Forums/hu/sharepoint2010general/thread/88b11fe3-c218-49a3-ac4b-d1a04939980c 感谢http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-Chttp://social.msdn.microsoft.com/Forums/hu/sharepoint2010general/thread/ 88b11fe3-c218-49a3-AC4B-d1a04939980c

just connect to the database and execute this query 只需连接到数据库并执行此查询

SELECT SERVERPROPERTY ('productversion'), 
       SERVERPROPERTY ('productlevel'), 
       SERVERPROPERTY ('edition')

references: http://support.microsoft.com/kb/321185 参考: http//support.microsoft.com/kb/321185

I hope that helps 我希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM