grass valley developers

Home > APIs > AppServer API > Examples > Asset Management > Query for Volume or Drive Letter

Query for Volume or Drive Letter

Below is sample code that shows you how to query for a system's drive letter(s). See why you don't want to hardcode the V: drive.

// setup a default volume string
string volume = "V:";

// create a MediaMgr object
IMediaMgr mediaMgr = iappServer.CreateMediaMgr(appName);

// get a cookie to an XML list representation of all volumes (drive letters) on the K2
int count = 0;
int cookie = mediaMgr.EnumerateVolumes(ref count);

// loop thru all volume results
int i = 0;
string result = "";
int maxCount = 256;
string xmlData = "";
count = maxCount;
while (count == maxCount)
 mediaMgr.GetXmlResults(cookie, i * maxCount, maxCount, ref count, out xmlData);
 result += xmlData;

// uncomment if you want to see the XML version of the enumerated volumes
// Console.WriteLine(result);

// NOTE: make sure your close the results set when you are finished! This 
// releases a database selector. Each MediaMgr is allowed to use up to ten 
// database selectors. If you don't close them, you will run out of selectors
// and your EnumerateX calls will fail.

// load the XML string into an XML document
XmlDocument xmlDoc = new XmlDocument();

// need a namespace manager to parse the xml doc
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
namespaceManager.AddNamespace("ns", "x-schema:#Schema1");

// loop thru all volumes looking for a V: or C: drive letter
XmlNodeList volumeList = xmlDoc.SelectNodes(("./Select/ns:Data/ns:Dataset"), namespaceManager);
if (volumeList.Count > 0)
 IEnumerator volumeNodeEnum = volumeList.GetEnumerator();
 bool bMoreVolumes = volumeNodeEnum.MoveNext();

 // look for the "DatasetName" (i.e drive letter) value.
 XmlNode volumeNode = (XmlNode)volumeNodeEnum.Current;
 string volumeName = volumeNode.Attributes.GetNamedItem("DatasetName").Value;

 // if we found a "V:" or "C:" value save it as the volume name
 if ( 0 == String.Compare( volumeName, "V:", true) ||
 0 == String.Compare( volumeName, "C:", true) )
 Console.WriteLine("Discovered drive " + volumeName);
 volume = volumeName;

 bMoreVolumes = volumeNodeEnum.MoveNext();
 } while (bMoreVolumes);
} // if volumeList.Count > 0