Nagendra Panyam

Nagendra Panyam

  • NA
  • 190
  • 17.6k

while loop modification,restructre the code

Feb 25 2019 1:40 AM
 is there any chance to restructure the fallowing code to improve performance....
 
 
public static string GetVolumeName(string devicePath)
{
_logger.CreateLog(Enumerations.LoggerEnum.Information, $"DiskLib::GetVolumeName: Input Device Path: {devicePath}");
_auditLogger.InsertAuditLog(LogStatus.Info, "GetVolumeName", "GetVolumeName", $"DiskLib::GetVolumeName: Input Device Path: {devicePath}");
string retVal = string.Empty;
StringBuilder sbVoluneName = new StringBuilder(uMax);
StringBuilder sbDevicePath = new StringBuilder(uMax);
Int64 handle = FindFirstVolume(sbVoluneName, uMax);
// return retVal;
if (handle != -1)
{
while (true)
{
int index = sbVoluneName.Length - 1;
if (sbVoluneName[0] != '\\' ||
sbVoluneName[1] != '\\' ||
sbVoluneName[2] != '?' ||
sbVoluneName[3] != '\\' ||
sbVoluneName[index] != '\\')
{
string tempName = sbVoluneName.ToString();
_logger.CreateLog(Enumerations.LoggerEnum.Error, $"DiskLib::GetVolumeName: FindFirstVolume/FindNextVolume function returned bad volume name: {tempName}");
_auditLogger.InsertAuditLog(LogStatus.Info, "GetVolumeName", "GetVolumeName", $"DiskLib::GetVolumeName: FindFirstVolume/FindNextVolume function returned bad volume name: {tempName}");
bool res1 = FindNextVolume(handle, sbVoluneName, uMax);
if (res1 == false)
{
break;
}
continue;
}
string vName = sbVoluneName.ToString();
string volName = sbVoluneName.ToString().Substring(4, index - 4);
_logger.CreateLog(Enumerations.LoggerEnum.Information, $"DiskLib::GetVolumeName: Volume Name: {volName}");
_auditLogger.InsertAuditLog(LogStatus.Info, "GetVolumeName", "GetVolumeName", $"DiskLib::GetVolumeName: Volume Name: {volName}");
int charCount = QueryDosDeviceA(volName, sbDevicePath, uMax);
if (charCount == 0)
{
_logger.CreateLog(Enumerations.LoggerEnum.Error, $"DiskLib::GetVolumeName: QueryDosDeviceW failed.");
_auditLogger.InsertAuditLog(LogStatus.Info, "GetVolumeName", "GetVolumeName", $"DiskLib::GetVolumeName: QueryDosDeviceW failed.");
bool res1 = FindNextVolume(handle, sbVoluneName, uMax);
if (res1 == false)
{
break;
}
continue;
}
string dPath = sbDevicePath.ToString();
_logger.CreateLog(Enumerations.LoggerEnum.Information, $"DiskLib::GetVolumeName: Device Path: {dPath}");
_auditLogger.InsertAuditLog(LogStatus.Info, "GetVolumeName", "GetVolumeName", $"DiskLib::GetVolumeName: Device Path: {dPath}");
if (dPath.Equals(devicePath))
{
retVal = volName;
break;
}
bool res = FindNextVolume(handle, sbVoluneName, uMax);
if (res == false)
break;
}
}
_logger.CreateLog(Enumerations.LoggerEnum.Information, $"DiskLib::GetVolumeName: Output Volume Name: {retVal}");
_auditLogger.InsertAuditLog(LogStatus.Info, "GetVolumeName", "GetVolumeName", $"DiskLib::GetVolumeName: Output Volume Name: {retVal}");
return retVal;
}

Answers (5)