디스크의 남은 용량을 나타내는 프로그램 코드입니다.
MainWindow.xaml
<Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <StackPanel x:Name="stackPanel"> <Label Content="전체 하드디스크 용량" HorizontalAlignment="Center" VerticalAlignment="Top"/> <Button x:Name="calButton" Content="계산하기" Width="200" Margin="0,0,0,20" Click="calButton_Click"/> </StackPanel> </Window>
|
MainWindow.xaml.cs
using System; using System.IO; using System.Windows; using System.Windows.Controls;
namespace WpfApp { /// <summary> /// MainWindow.xaml에 대한 상호 작용 논리 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); }
/// <summary> /// 계산하기 버튼 클릭 이벤트입니다. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void calButton_Click(object sender, RoutedEventArgs e) { DriveInfo[] driveInfos = DriveInfo.GetDrives();
foreach (DriveInfo driveInfo in driveInfos) { if (driveInfo.DriveType == DriveType.Fixed) { ProgressBar progressBar = new ProgressBar() { HorizontalAlignment = HorizontalAlignment.Center };
// 전체 용량을 계산합니다. progressBar.Maximum = Convert.ToInt32(driveInfo.TotalSize / 1024 / 1024);
// 남은 용량을 계산합니다. (MB) progressBar.Value = Convert.ToInt32(driveInfo.TotalSize / 1024 / 1024) - Convert.ToInt32(driveInfo.AvailableFreeSpace / 1024 / 1024);
progressBar.Width = 200;
progressBar.Height = 20;
this.stackPanel.Children.Add(progressBar);
Label label = new Label() { HorizontalAlignment = HorizontalAlignment.Center };
label.Content = driveInfo.Name + " " + (driveInfo.TotalFreeSpace / 1024 / 1024) + "MB Free";
this.stackPanel.Children.Add(label); } } } } }
|
계산하기 버튼을 클릭하면 디스크 남은 용량이 나열됩니다.
'C# > WPF' 카테고리의 다른 글
[WPF] ListBox 에 색 바인딩하고 선택 후 스크롤하기 (0) | 2020.07.06 |
---|---|
[WPF] 상단 검은색 메뉴? 제거 하기 (런타임 도구 제거) (0) | 2020.07.06 |
[WPF] DrawingContext 영역을 벗어나지 않게 클리핑 영역 설정하기 (0) | 2020.07.05 |
[WPF] 비정상 종료 막기 (0) | 2020.07.02 |
[WPF] Panel 내의 Button 에 같은 이벤트 적용하기 (0) | 2020.07.01 |