This code is supposed to enable FILESTREAM on a SQL Service. There is a manual process to do this, but we really need an automated way. I found this code on another help site. People have been able to get it to work but I am receiving the error below.
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Management.dll
private void EnableFilestream(int accessLevel)
{
ManagementObject filestreamSettingsObject = GetFilestreamManagementObject("WIN-D0RFC79OHJ7", "MSSQLSERVER");
ManagementBaseObject methodArgs = filestreamSettingsObject.GetMethodParameters("EnableFilestream");
methodArgs["AccessLevel"] = accessLevel;
methodArgs["ShareName"] = ""; //default
ManagementBaseObject returnObject = filestreamSettingsObject.InvokeMethod("EnableFilestream", methodArgs, null);
uint returnValue = (uint)returnObject.GetPropertyValue("ReturnValue");
const uint errorSuccessRestartRequired = 0x80070BC3;
if (returnValue != 0 && returnValue != errorSuccessRestartRequired)
{
Win32Exception win32Exception = new Win32Exception((int)returnValue);
string exceptionText =
string.Format("'EnableFilestream' method returned {0}: {1}", returnValue, win32Exception.Message);
}
}
private ManagementObject GetFilestreamManagementObject(string machineName, string instanceName)
{
string managementPath = string.Format(@"\\{0}\root\Microsoft\SqlServer\ComputerManagement10", machineName);
ManagementScope managementScope = new ManagementScope(managementPath);
managementScope.Connect();
SelectQuery query = new SelectQuery("FilestreamSettings", string.Format("InstanceName='{0}'", instanceName));
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(managementScope, query))
{
ManagementObjectCollection moc = searcher.Get();
if (1 != moc.Count)
{
string exceptionText = String.Format("Expected single instance of FilestreamSettings WMI object, found {0}.", moc.Count);
}
ManagementObjectCollection.ManagementObjectEnumerator enumerator = moc.GetEnumerator();
return (ManagementObject)enumerator.Current;
}
}
I believe the issue is occurring on line:
ManagementObjectCollection moc = searcher.Get();
I cannot be fully sure though since it does not actually crash the application. Adding this exception in the debug properties also did not cause it to "break." I am also running this as administrator. Any help is appreciated. If you need anymore information, please let me know.
User contributions licensed under CC BY-SA 3.0