How to use c# creat dns server zone ?

  • Thread starter Thread starter huang hspeeder via DotNetMonster.com
  • Start date Start date
H

huang hspeeder via DotNetMonster.com

HI
I try it code only creat DNS ResourceRecord,How to creat dns zone?
thanks!

this code:

public void CreateRR(string zoneName, string rrTextRep)
{
if ( zoneName == null )
throw new ArgumentNullException("zoneName", "zoneName is null.");
if ( rrTextRep == null )
throw new ArgumentNullException("rrTextRep", "rrTextRep is null.");

InvokeMethodOptions options = new InvokeMethodOptions();
options.Timeout = new TimeSpan(0,0,0,5);

ManagementClass rrClass = new
ManagementClass(@"\\"+ System.Environment.MachineName
+@"\root\MicrosoftDNS","MicrosoftDNS_ResourceRecord",null);

//Get an input parameters object for this method
ManagementBaseObject inParams =
rrClass.GetMethodParameters("CreateInstanceFromTextRepresentation");

//Fill in input parameter values
inParams["DnsServerName"] = null; //Don't need.
inParams["ContainerName"] = zoneName;
inParams["TextRepresentation"] = rrTextRep;

//Execute the method
try
{
ManagementBaseObject outObject = rrClass.InvokeMethod
("CreateInstanceFromTextRepresentation", inParams, null);
PropertyDataCollection outProperties = outObject.Properties;
foreach (PropertyData outProperty in outProperties)
{
Console.WriteLine("Property = " + outProperty.Name);
}
}
catch
{
throw new ApplicationException
("CreateInstanceFromTextRepresentationmethod failed.");
}
} // End CreateInstanceFromTextRepresentation Method.
 
Try this...

//Create Foward Lookup Zone.
string sServerPath = "\\\\localhost\\ROOT\\MicrosoftDNS";
ManagementScope oScope = new ManagementScope(sServerPath);
ManagementPath Path = new ManagementPath("MicrosoftDNS_Zone");
ManagementClass DnsZoneClass = new ManagementClass(oScope, Path, null);
ManagementBaseObject InputParams =
DnsZoneClass.GetMethodParameters("CreateZone");
InputParams["ZoneName"] = "sample.com";
InputParams["ZoneType"] = Convert.ToInt32(0);
InputParams["DataFileName"] = "sample.com.dns";
ManagementBaseObject OutParams = DnsZoneClass.InvokeMethod("CreateZone",
InputParams, null);

good luck!

Jamie
 
Back
Top