반응형

- 코드

using System.IO;

FileInfo fileInfo = new FileInfo(@"D:\ubuntu-20.04.2-live-server-amd64.iso");

if (fileInfo.Exists)
{
    Debug.WriteLine("File FullName : " + fileInfo.FullName);
    Debug.WriteLine("File Name : " + fileInfo.Name);
    Debug.WriteLine("File Name : " + fileInfo.Extension);    
    Debug.WriteLine("File Length : " + fileInfo.Length + " byte");

    Debug.WriteLine("File CreationTime : " + fileInfo.CreationTime.ToString());
    Debug.WriteLine("File LastWriteTime : " + fileInfo.LastWriteTime.ToString());

    string copyFilePath = @"D:\ubuntu-20.04.2-live-server-amd64_Copy.iso";
    fileInfo.CopyTo(copyFilePath);
    //fileInfo.CopyTo(copyFilePath, true);

    fileInfo.Delete();
}

 

- 실행 결과

File FullName : D:\ubuntu-20.04.2-live-server-amd64.iso
File Name : ubuntu-20.04.2-live-server-amd64.iso
File Name : .iso
File Length : 1215168512 byte
File CreationTime : 2021-05-19 오후 5:16:41
File LastWriteTime : 2021-04-12 오후 7:51:25

 

- 파일 크기

public long Length { get; }

 

- 파일 전체 경로, 파일 이름, 파일 확장명

// 파일 전체 경로
public virtual string FullName { get; }

// 파일 이름
public abstract string Name { get; }

// 파일의 확장명
public string Extension { get; }

 

- 파일 복사

// 파일 복사
public System.IO.FileInfo CopyTo (string destFileName, bool overwrite);

bool overwrite 
> true : 덮어 쓰기
> false : 덮어 쓰지 않음

 

- 파일 삭제

public override void Delete();
반응형
,
반응형

 

 

- 코드

using System.Diagnostics; // Debug.WriteLine 사용 용도

System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();

if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    Debug.WriteLine(fbd.SelectedPath);
}

 

System.Windows.Forms.FolderBrowserDialog를 사용하기 위해서는 솔루션 탐색기에서 솔루션의 참조를 마우스 오른쪽 버튼을 클릭하여 참조 관리자를 열어 나오는 목록에서  <System.Windows.Forms> 체크하고 확인 버튼을 클릭하면 돼요.

 

만약에 참조 추가를 해주지 않고 빌드하면 아래처럼 오류가 발생하니 참고하세요.

 

에러 메시지 : <error CS0234: 'System.Windows' 네임스페이스에 'Forms' 형식 또는 네임스페이스 이름이 없습니다. 어셈블리 참조가 있는지 확인하세요.>

 

- 실행 결과

D:\

 

- 버튼 리턴 값 (System.Windows.Forms.DialogResult)

    //
    // 요약:
    //     대화 상자의 반환 값을 나타내는 식별자를 지정 합니다.
    [ComVisible(true)]
    public enum DialogResult
    {
        //
        // 요약:
        //     Nothing 대화 상자에서 반환 됩니다. 즉, 모달 대화 상자 실행을 계속 합니다.
        None = 0,
        //
        // 요약:
        //     대화 상자의 반환 값은 OK (일반적으로 확인 레이블이 붙은 단추에서 보냄).
        OK = 1,
        //
        // 요약:
        //     대화 상자의 반환 값은 Cancel (일반적으로 Cancel 레이블이 붙은 단추에서 보냄).
        Cancel = 2,
        //
        // 요약:
        //     대화 상자의 반환 값은 Abort (일반적으로 중단 레이블이 붙은 단추에서 보냄).
        Abort = 3,
        //
        // 요약:
        //     대화 상자의 반환 값은 Retry (일반적으로 재시도 레이블이 붙은 단추에서 보냄).
        Retry = 4,
        //
        // 요약:
        //     대화 상자의 반환 값은 Ignore (일반적으로 무시 레이블이 붙은 단추에서 보냄).
        Ignore = 5,
        //
        // 요약:
        //     대화 상자의 반환 값은 Yes (일반적으로 예 레이블이 붙은 단추에서 보냄).
        Yes = 6,
        //
        // 요약:
        //     대화 상자의 반환 값은 No (일반적으로 아니요 레이블이 붙은 단추에서 보냄).
        No = 7
    }
반응형
,
반응형

 

이번에는 파일 선택 다일로그를 여는 방법에 대해서 알아보도록 할게요.

 

 

- 소스 코드

System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
dialog.Filter = "JPEG|*.jpg;*.jpeg|All files(*.*)|*.*";

if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
   Debug.WriteLine(dialog.FileName);
}
else
{
   return;
}

 

- 실행 결과

E:\제목 없음.jpg

 

dialog.Filter에 파일 필터를 넣어 해당 확장자를 가진 파일만 선택가능하게 설정할 수 있어요.

 

- 이름|확장명 : JPEG|*.jpg

- 파일 확장명 상관없이 전체 파일 : All files(*.*)|*.*

 

 

파일 선택 다이얼로그(dialog.ShowDialog())에서 열기 버튼을 클릭하면 <System.Windows.Forms.DialogResult.OK>가 리턴되며, 닫기 버튼을 클릭할 경우 <System.Windows.Forms.DialogResult.Cancel>가 반환돼요.

 

//
    // 요약:
    //     대화 상자의 반환 값을 나타내는 식별자를 지정 합니다.
    [ComVisible(true)]
    public enum DialogResult
    {
        //
        // 요약:
        //     Nothing 대화 상자에서 반환 됩니다. 즉, 모달 대화 상자 실행을 계속 합니다.
        None = 0,
        //
        // 요약:
        //     대화 상자의 반환 값은 OK (일반적으로 확인 레이블이 붙은 단추에서 보냄).
        OK = 1,
        //
        // 요약:
        //     대화 상자의 반환 값은 Cancel (일반적으로 Cancel 레이블이 붙은 단추에서 보냄).
        Cancel = 2,
        //
        // 요약:
        //     대화 상자의 반환 값은 Abort (일반적으로 중단 레이블이 붙은 단추에서 보냄).
        Abort = 3,
        //
        // 요약:
        //     대화 상자의 반환 값은 Retry (일반적으로 재시도 레이블이 붙은 단추에서 보냄).
        Retry = 4,
        //
        // 요약:
        //     대화 상자의 반환 값은 Ignore (일반적으로 무시 레이블이 붙은 단추에서 보냄).
        Ignore = 5,
        //
        // 요약:
        //     대화 상자의 반환 값은 Yes (일반적으로 예 레이블이 붙은 단추에서 보냄).
        Yes = 6,
        //
        // 요약:
        //     대화 상자의 반환 값은 No (일반적으로 아니요 레이블이 붙은 단추에서 보냄).
        No = 7
    }

 

그럼 이상으로 포스팅을 마치며 오늘도 즐프하세요.

 

반응형
,
반응형

 

C#, WPF 기본 버튼의 스타일은 모서리가 네모 모양인데, 이를 둥글게 또는 원형 버튼으로 만드는 방법에 대해서 알아보도록 할게요.

 

 

XAML에서 다음과 같이 코드를 수정하면 버튼의 모서리를 둥글게 만들 수 있어요.

 

- 일반 버튼

 

<Button Width="100" Height="30" Content="일반 버튼"/>

 

- 모서리가 둥근 버튼

 

<Button Width="100" Height="30" Content="모서리 둥근 버튼">
   <Button.Resources>
      <Style TargetType="Border">
         <Setter Property="CornerRadius" Value="5"/>
      </Style>
   </Button.Resources>
</Button>

 

- 타원형의 버튼

 

<Button Width="100" Height="30" Content="원 버튼">
   <Button.Resources>
      <Style TargetType="Border">
         <Setter Property="CornerRadius" Value="50"/>
      </Style>
   </Button.Resources>
</Button>

 

CornerRadius의 Value의 숫자가 높을 수록 모서리가 둥글해지는 것을 확인할 수 있으며, 이를 통해 원형 모양의 버튼도 만들 수 있어요.

 

- 전체 소스 코드

 

<StackPanel Orientation="Horizontal" Margin="20" VerticalAlignment="Top">
        <Button Width="100" Height="30" Content="일반 버튼"/>

        <Grid Width="10"/>

        <Button Width="100" Height="30" Content="모서리 둥근 버튼">
            <Button.Resources>
                <Style TargetType="Border">
                    <Setter Property="CornerRadius" Value="5"/>
                </Style>
            </Button.Resources>
        </Button>


        <Grid Width="10"/>

        <Button Width="100" Height="30" Content="원 버튼">
            <Button.Resources>
                <Style TargetType="Border">
                    <Setter Property="CornerRadius" Value="50"/>
                </Style>
            </Button.Resources>
        </Button>
</StackPanel>

스타일 적용 시에는 모든 버튼 또는 해당 스타일을 적용한 버튼에 일괄 적용도 가능해요.

 

아래 스타일 적용 시에는 마우스 포인트가 버튼 위에 올라가면 나오는 마우스 오버 효과 등은 별도로 정의를 해줘야 하는니 참고하세요. (버튼 클릭 이벤트 등은 정상적으로 동작)

 

- 스타일 코드

 

  <Window.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border CornerRadius="5" BorderBrush="DarkGray" Background="{TemplateBinding Background}" BorderThickness="1">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="RoundButtonStyleBorderBrushRed" TargetType="Button">
            <Setter Property="Background" Value="LightGray"/>
            <Setter Property="Foreground" Value="Black" />

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border CornerRadius="5" BorderBrush="Red" Background="{TemplateBinding Background}" BorderThickness="1">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel Orientation="Horizontal" Margin="20" VerticalAlignment="Top">
        <Button Width="100" Height="30" Content="일반 버튼"/>

        <Grid Width="10"/>
        <Button Width="100" Height="30" Content="스타일 적용"
                Style="{StaticResource RoundButtonStyleBorderBrushRed}"/>
    </StackPanel>

 

- 마우스 오버 색상 지정 코드

  (Background의 Value을 원하는 색상으로 변경)

 

<Style.Triggers>
   <Trigger Property="IsMouseOver" Value="True" >
      <Setter Property="Background" Value="#FF81C2E8" />
   </Trigger>
</Style.Triggers>

 

그럼 이상으로 포스팅을 마치며 오늘도 즐프하세요.

 

반응형
,
반응형

 

* 코드 - 자기 자신(실행 중인 프로그램)의 실행 중인 경로 가져오기

string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
Debug.WriteLine(path);  // 현재 실행중인 프로그램 명을 포함한 경로

path = System.IO.Path.GetDirectoryName(path);
Debug.WriteLine(path);   // 현재 실행중인 프로그램의 경로

 

* 실행 결과

D:\Test\WPF\WPF\bin\Debug\WPF_TEST.exe
D:\Test\WPF\WPF\bin\Debug

 

System.Reflection.Assembly.GetExecutingAssembly().Location; 는 현재 이 코드를 실행한 exe 파일과 확장명을 포함하여 출력하여 경로만 필요하신 분은 System.IO.Path.GetDirectoryName();를 이용하시면 됩니다.

 

반응형
,