6
Answers

working with drives

foreach (DiskinfoDetails item in diskList1)
{
if (item.Status.Equals("Unencrypted") || item.Status.Equals("Encryption paused") || item.Status.Equals("Encryption in-progress"))
{
continue;
}
if (result == 3)
{
SystemStateInfo systemStateInfo = SystemDataLib.GetSystemState();
systemStateInfo.SystemState = ENUMSYSTEMSTATE.ENCRYPTION_COMPLETED;
SystemDataLib.SaveSystemState(systemStateInfo);
SystemDataLib.UpdateDiskStatus("Encryption Completed");
//diskList2.Add(item);
// Refresh Grid
//DriveInformation.Clear();
var data = GetSystemInfoHelper.GetPartitons();
foreach (var diskInformation in data)
{
if (bootDrive != null && item.DriveLetter == bootDrive.Replace(":", ""))
{
item.DriveSize = diskInformation.DriveSize;
item.DiskLabel = diskInformation.DiskLabel;
item.DevicePath = diskInformation.DevicePath;
item.Status = diskInformation.Status;
item.IsWindowsDrive = diskInformation.IsWindowsDrive;
break;
}
}
}
if (result == 4)
{
SystemStateInfo systemStateInfo = SystemDataLib.GetSystemState();
systemStateInfo.SystemState = ENUMSYSTEMSTATE.DECRYPTION_INPROGRESS;
SystemDataLib.SaveSystemState(systemStateInfo);
SystemDataLib.UpdateDiskStatus("Decryption In-progress");
//diskList2.Add(item);
// Refresh Grid
//DriveInformation.Clear();
var data = GetSystemInfoHelper.GetPartitons();
foreach (var diskInformation in data)
{
if (bootDrive != null && item.DriveLetter == bootDrive.Replace(":", ""))
{
item.DriveSize = diskInformation.DriveSize;
item.DiskLabel = diskInformation.DiskLabel;
item.DevicePath = diskInformation.DevicePath;
item.Status = diskInformation.Status;
item.IsWindowsDrive = diskInformation.IsWindowsDrive;
break;
}
}
}
if (result == 1)
{
SystemStateInfo systemStateInfo = SystemDataLib.GetSystemState();
systemStateInfo.SystemState = ENUMSYSTEMSTATE.DECRYPTION_COMPLETED;
SystemDataLib.SaveSystemState(systemStateInfo);
SystemDataLib.UpdateDiskStatus("Unencrypted");
BootDriveSystemSateInfo bootDriveSystemSate = SystemDataLib.GetBootDriveSystemSate();
bootDriveSystemSate.SystemState = ENUMSYSTEMSTATE.DECRYPTION_COMPLETED;
SystemDataLib.SaveBootDriveSystemState(bootDriveSystemSate);
var data = GetSystemInfoHelper.GetPartitons();
foreach (var diskInformation in data)
{
if (bootDrive != null && item.DriveLetter == bootDrive.Replace(":", ""))
{
item.DriveSize = diskInformation.DriveSize;
item.DiskLabel = diskInformation.DiskLabel;
item.DevicePath = diskInformation.DevicePath;
item.Status = diskInformation.Status;
item.IsWindowsDrive = diskInformation.IsWindowsDrive;
break;
}
}
}
diskList2.Add(item);
}
 
 
 
can i modify above logic to any simple,bcause it tooks more time to get system drives 
Answers (6)
1
Salman Beg

Salman Beg

151 12.1k 621.4k 6y
Hi. I Refactored the whole code and which is given below,
  1. foreach (DiskinfoDetails item in diskList1)  
  2.             {  
  3.                 if (item.Status.Equals("Unencrypted") || item.Status.Equals("Encryption paused") || item.Status.Equals("Encryption in-progress"))  
  4.                 {  
  5.                     continue;  
  6.                 }  
  7.                 SystemStateInfo systemStateInfo = SystemDataLib.GetSystemState();  
  8.                 if (result == 3)  
  9.                 {                      
  10.                     systemStateInfo.SystemState = ENUMSYSTEMSTATE.ENCRYPTION_COMPLETED;  
  11.                     SystemDataLib.SaveSystemState(systemStateInfo);  
  12.                     SystemDataLib.UpdateDiskStatus("Encryption Completed");                                          
  13.                 }  
  14.                 if (result == 4)  
  15.                 {  
  16.                     systemStateInfo.SystemState = ENUMSYSTEMSTATE.DECRYPTION_INPROGRESS;  
  17.                     SystemDataLib.SaveSystemState(systemStateInfo);  
  18.                     SystemDataLib.UpdateDiskStatus("Decryption In-progress");                      
  19.                 }  
  20.                 if (result == 1)  
  21.                 {  
  22.                     systemStateInfo.SystemState = ENUMSYSTEMSTATE.DECRYPTION_COMPLETED;  
  23.                     SystemDataLib.SaveSystemState(systemStateInfo);  
  24.                     SystemDataLib.UpdateDiskStatus("Unencrypted");  
  25.                     BootDriveSystemSateInfo bootDriveSystemSate = SystemDataLib.GetBootDriveSystemSate();  
  26.                     bootDriveSystemSate.SystemState = ENUMSYSTEMSTATE.DECRYPTION_COMPLETED;  
  27.                     SystemDataLib.SaveBootDriveSystemState(bootDriveSystemSate);                      
  28.                 }  
  29.                 var data = GetSystemInfoHelper.GetPartitons();  
  30.                 foreach (var diskInformation in data)  
  31.                 {  
  32.                     if (bootDrive != null && item.DriveLetter == bootDrive.Replace(":"""))  
  33.                     {  
  34.                         item.DriveSize = diskInformation.DriveSize;  
  35.                         item.DiskLabel = diskInformation.DiskLabel;  
  36.                         item.DevicePath = diskInformation.DevicePath;  
  37.                         item.Status = diskInformation.Status;  
  38.                         item.IsWindowsDrive = diskInformation.IsWindowsDrive;  
  39.                         break;  
  40.                     }  
  41.                 }  
  42.                 diskList2.Add(item);  
  43.             }  
 
If it helps mark as answer.
Thanks. 
Accepted
0
Salman Beg

Salman Beg

151 12.1k 621.4k 6y
You have so many duplicate codes so i just removed.
  1. SystemStateInfo systemStateInfo = SystemDataLib.GetSystemState();  
The above line wrote multiple times in the if clause and which is absolutely necessary. Only One time write is enough for this code.
The foreach loop you wrote multiple times in the if clause and in place of that i wrote it after all the if clauses. 
Thanks. 
0
Nagendra Panyam

Nagendra Panyam

NA 190 18.8k 6y
can u elobrate me how u approach that one please 
0
Nagendra Panyam

Nagendra Panyam

NA 190 18.8k 6y
my superior tells that unnessarly written foreach loop is there try to avoid foreach loop,they tells that issue not related to threads issue, issue is related to performance issue,unnessarly calling methods duplicate statements are written you have to remove them and improve the performance .they given that hint only
0
Nagendra Panyam

Nagendra Panyam

NA 190 18.8k 6y
am a beginer can u help me to learn above program overall explanation what its doing or can you help me understanding way what c# concepts involved in above progarm what synatxes is help ful to me ,help me to sort out 
0
Amit Gupta

Amit Gupta

NA 22.9k 247.1k 6y
I think your code contains many methods that used to process the commands. You can check those functions or use async await programming to do this in background thread.