버튼을 눌러 .txt나 .xlsx같은 확장자를 가진 파일을 연다.
https://velog.io/@yu_oolong/C-WPF-MVVM-패턴으로-프로젝트-시작하기 와 같이 프로젝트를 생성한다.
폴더 구조는 다음과 같다.
버튼을 눌러서 외부 파일을 실행하기 위해 화면에 Button 컨트롤을 배치한다.
<Page x:Class="PracticeProject2.Views.Page5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PracticeProject2.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page5"
Background="White">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Content="Open File" Width="100" Height="50" Cursor="Hand"/>
</StackPanel>
</Grid>
</Page>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace PackagingSystem.Commands
{
public class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
public RelayCommand(Action execute, Func<bool> canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter) => _canExecute == null || _canExecute();
public void Execute(object parameter) => _execute();
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace PracticeProject2.ViewModels
{
internal class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetProperty<T>(ref T backingField, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(backingField, value)) return false;
backingField = value;
OnPropertyChanged(propertyName);
return true;
}
}
}
using PracticeProject2.Commands;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace PracticeProject2.ViewModels
{
internal class Page5ViewModel : BaseViewModel
{
public ICommand openFileCommand { get; set; }
public Page5ViewModel()
{
openFileCommand = new RelayCommand(openFile);
}
private void openFile()
{
string directory = @"D:\\Edu\\";
string filename = "memo.txt";
string filepath = directory + filename;
Process.Start(filepath);
}
}
}
<Page x:Class="PracticeProject2.Views.Page5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:PracticeProject2.ViewModels"
xmlns:local="clr-namespace:PracticeProject2.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page5"
Background="White">
<Page.DataContext>
<vm:Page5ViewModel />
</Page.DataContext>
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Content="Open File" Width="100" Height="50" Cursor="Hand" Command="{Binding openFileCommand}"/>
</StackPanel>
</Grid>
</Page>
실행 후 버튼을 클릭하면 경로로 지정한 파일인 memo.txt 메모장 파일이 실행됐다.
.xlsx 파일을 경로로 지정하면 엑셀이 실행된다.
Process.Start 메서드