Windows 8開發入門(十三) windows 8的檔案及檔案夾管理 2

來源:互聯網
上載者:User

檔案以及檔案夾操作

在本文中我們將學習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 = "提示:未找到該檔案,請先建立檔案";
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.