Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. Drive usage info shows correctly. But when I switch off of the drives the disk usage info is not updated. I can't figure out why.
  2.  
  3. <local1:Drives x:Key="drivesUsage" />
  4.  
  5. <StackPanel
  6. Name="stkDrives"
  7. Grid.Row="3"
  8. Grid.ColumnSpan="2"
  9. DataContext="{StaticResource drivesUsage}"
  10. Orientation="Horizontal">
  11. <Label Margin="10" VerticalAlignment="Center">
  12. C:
  13. </Label>
  14. <TextBox
  15. Name="txbDriveC"
  16. Margin="2"
  17. VerticalAlignment="Center"
  18. Style="{StaticResource txbStyle}"
  19. Text="{Binding DriveC}" />
  20. <Label Margin="15" VerticalAlignment="Center">
  21. D:
  22. </Label>
  23. <TextBox
  24. Name="txbDriveD"
  25. Margin="2"
  26. VerticalAlignment="Center"
  27. Style="{StaticResource txbStyle}"
  28. Text="{Binding DriveD}" />
  29. <Label Margin="15" VerticalAlignment="Center">
  30. I:
  31. </Label>
  32. <TextBox
  33. Name="txbDriveI"
  34. Margin="2"
  35. VerticalAlignment="Center"
  36. Style="{StaticResource txbStyle}"
  37. Text="{Binding DriveI}" />
  38. <Label Margin="15" VerticalAlignment="Center">
  39. H:
  40. </Label>
  41. <TextBox
  42. Name="txbDriveH"
  43. Margin="2"
  44. VerticalAlignment="Center"
  45. Style="{StaticResource txbStyle}"
  46. Text="{Binding DriveH}" />
  47. </StackPanel>
  48.  
  49. public partial class MainWindow : Window
  50. {
  51. public MainWindow()
  52. {
  53.  
  54.  
  55. InitializeComponent();
  56.  
  57. Timer myTimer = new Timer(1000);
  58. myTimer.Start();
  59. myTimer.Elapsed += new ElapsedEventHandler(timeToCall);
  60.  
  61. }
  62.  
  63.  
  64. private void timeToCall(object sender, ElapsedEventArgs e)
  65. {
  66. DrivesCheck();
  67. }
  68.  
  69. private void DrivesCheck()
  70. {
  71. Drives d = new Drives();
  72. }
  73.  
  74. }
  75. public class Drives : INotifyPropertyChanged
  76.  
  77. {
  78. public event PropertyChangedEventHandler PropertyChanged;
  79.  
  80. private void OnPropertyChanged(string property)
  81. {
  82. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
  83. }
  84.  
  85. private string driveC;
  86. private string driveD;
  87. private string driveI;
  88. private string driveH;
  89.  
  90. public string DriveC
  91. {
  92. get { return driveC; }
  93. set
  94. {
  95. driveC = value;
  96. OnPropertyChanged(nameof(DriveC));
  97. }
  98. }
  99.  
  100. public string DriveD
  101. {
  102. get { return driveD; }
  103. set
  104. {
  105. driveD = value;
  106. OnPropertyChanged(nameof(DriveD));
  107. }
  108. }
  109.  
  110. public string DriveI
  111. {
  112. get { return driveI; }
  113. set
  114. {
  115. driveI = value;
  116. OnPropertyChanged(nameof(DriveI));
  117. }
  118. }
  119.  
  120. public string DriveH
  121. {
  122. get { return driveH; }
  123. set
  124. {
  125. driveH = value;
  126. OnPropertyChanged(nameof(DriveH));
  127. }
  128. }
  129.  
  130. private List<Tuple<string, double>> diskUsage()
  131. {
  132. var result = new List<Tuple<string, double>>(4);
  133.  
  134. DriveInfo[] allDrives = DriveInfo.GetDrives()
  135. .Where(p => p.DriveType == DriveType.Fixed)
  136. .Select(p => p).ToArray();
  137.  
  138. foreach (DriveInfo d in allDrives)
  139. {
  140. var driveName = d.Name.Substring(0, 2);
  141.  
  142. if (d.IsReady == true)
  143. {
  144. var freeSpace = Math.Round(d.TotalFreeSpace / Math.Pow(1024, 3));
  145.  
  146. result.Add(new Tuple<string, double>(driveName, freeSpace));
  147. }
  148. }
  149. return result;
  150. }
  151.  
  152. public Drives()
  153. {
  154. var result = diskUsage();
  155.  
  156. int size = result.Count;
  157.  
  158. switch (size)
  159. {
  160. case 4:
  161. DriveC = result[0].Item2.ToString();
  162. DriveD = result[1].Item2.ToString();
  163. DriveI = result[2].Item2.ToString();
  164. DriveH = result[3].Item2.ToString();
  165. break;
  166.  
  167. case 3:
  168. DriveC = result[0].Item2.ToString();
  169. DriveD = result[1].Item2.ToString();
  170. DriveI = result[2].Item2.ToString();
  171. DriveH = "0";
  172. break;
  173.  
  174. case 2:
  175. DriveC = result[0].Item2.ToString();
  176. DriveD = result[1].Item2.ToString();
  177. DriveI = "0";
  178. DriveH = "0";
  179. break;
  180.  
  181. case 1:
  182. DriveC = result[0].Item2.ToString();
  183. DriveD = "0";
  184. DriveI = "0";
  185. DriveH = "0";
  186. break;
  187.  
  188. default:
  189. break;
  190. }
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement