檔案以及檔案夾操作
在本文中我們將學習win 8中的檔案以及檔案夾的各種操作。
在本文中檔案操作主要是講述:刪 除檔案/移動檔案/複製檔案/重新命名檔案
檔案夾操作分為:讀取檔案夾/建立檔案夾/刪除檔案夾/重 命名檔案夾
首先貼出所有的Xaml代碼檔案部分:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <!--顯示區--> <TextBlock HorizontalAlignment="Left" Margin="137,42,0,0" TextWrapping="Wrap" Text="檔案名稱:" VerticalAlignment="Top" Height="23" Width="43"/> <TextBox HorizontalAlignment="Left" Margin="185,33,0,0" TextWrapping="Wrap" Text="test.txt" VerticalAlignment="Top" Width="121" Name="tbFileName"/> <TextBox HorizontalAlignment="Left" Margin="457,33,0,0" TextWrapping="Wrap" Text="預設需要添加的檔案內容" VerticalAlignment="Top" Width="431" Name="tbContent"/> <TextBlock HorizontalAlignment="Left" Margin="396,42,0,0" TextWrapping="Wrap" Text="檔案內容:" VerticalAlignment="Top" Height="23" Width="61"/> <TextBlock HorizontalAlignment="Left" Margin="127,163,0,0" TextWrapping="Wrap" Text="提示:" VerticalAlignment="Top" Height="23" Width="761" Name="tb_show"/> <!--刪除檔案 移動檔案 複製檔案 重新命名檔案--> <Button Content="建立並寫入檔案" HorizontalAlignment="Left" Margin="127,99,0,0" Name="btnCreateFile" VerticalAlignment="Top" Click="btnCreateFile_Click"/> <Button Content="讀取string檔案" HorizontalAlignment="Left" Margin="757,99,0,0" x:Name="btnReadFile" VerticalAlignment="Top" Click="btnReadFile_Click"/> <Button Content="刪除檔案" HorizontalAlignment="Left" Margin="127,223,0,0" x:Name="btnDeleteFile" VerticalAlignment="Top" Click="btnDeleteFile_Click"/> <Button Content="移動檔案" HorizontalAlignment="Left" Margin="320,223,0,0" x:Name="btnMoveFile" VerticalAlignment="Top" Click="btnMoveFile_Click"/> <Button Content="複製檔案" HorizontalAlignment="Left" Margin="560,223,0,0" x:Name="btnCopyFile" VerticalAlignment="Top" Click="btnCopyFile_Click"/> <Button Content="重新命名檔案" HorizontalAlignment="Left" Margin="780,223,0,0" x:Name="btnReNameFile" VerticalAlignment="Top" Click="btnReNameFile_Click"/> <!--讀取檔案夾 建立檔案夾 刪除檔案夾 重新命名檔案夾--> <Button Content="讀取檔案夾" HorizontalAlignment="Left" Margin="127,296,0,0" VerticalAlignment="Top" Name="readFolder" Click="readFolder_Click"/> <Button Content="建立檔案夾" HorizontalAlignment="Left" Margin="305,296,0,0" VerticalAlignment="Top" x:Name="btnCreateFolder" Click="btnCreateFolder_Click"/> <Button Content="刪除檔案夾" HorizontalAlignment="Left" Margin="545,296,0,0" VerticalAlignment="Top" x:Name="btnDeleteFolder" Click="btnDeleteFolder_Click"/> <Button Content="重新命名檔案夾" HorizontalAlignment="Left" Margin="766,296,0,0" VerticalAlignment="Top" x:Name="btnReNameFolder" Click="btnReNameFolder_Click"/> </Grid>
其次我們來看刪除檔案/移動檔案/複製檔案/重新命名檔案的Cs代碼:
private async void btnDeleteFile_Click(object sender, RoutedEventArgs e)
{
try
{
StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
await sf.DeleteAsync();
tb_show.Text = "提示:" + this.tbFileName.Text.Trim() + "檔案刪除成功!";
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到該檔案,請先建立檔案";
}
}
private async void btnMoveFile_Click(object sender, RoutedEventArgs e)
{
try
{
//將檔案從文檔移動到音樂庫
StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
StorageFolder newfolder = KnownFolders.MusicLibrary;
await sf.MoveAsync(newfolder, "moveFile.txt", NameCollisionOption.ReplaceExisting);
tb_show.Text = "提示:“庫\\文檔\\" + this.tbFileName.Text.Trim() + "”檔案移動到“庫\\音樂\\moveFile.txt”";
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到該檔案,請先建立檔案";
}
}
private async void btnCopyFile_Click(object sender, RoutedEventArgs e)
{
try
{
StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
StorageFile storageFileCopy = await sf.CopyAsync(KnownFolders.DocumentsLibrary, "copyFile.txt",
NameCollisionOption.ReplaceExisting);
tb_show.Text = "提示:“庫\\文檔\\" + this.tbFileName.Text.Trim() + "”檔案拷貝一份到“庫\\文檔\\copyFile.txt”";
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到該檔案,請先建立檔案";
}
}
private async void btnReNameFile_Click(object sender, RoutedEventArgs e)
{
try
{
StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
await sf.RenameAsync("renameFile.txt", NameCollisionOption.ReplaceExisting);
tb_show.Text = "提示:“庫\\文檔\\" + this.tbFileName.Text.Trim() + "”檔案重新命名為“庫\\文檔\\renameFile.txt”";
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到該檔案,請先建立檔案";
}
}