Windows 8实用窍门系列:13.windows 8的文件.文件夹管理---2.文件以及文件夹操作

简介:

  在本文中我们将学习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 = "提示:未找到该文件,请先创建文件";
            }
        }
复制代码

  最后我们来看文件夹操作读取文件夹/创建文件夹/删除文件夹/重命名文件夹的Cs代码如下:

复制代码
        private async void readFolder_Click(object sender, RoutedEventArgs e)
        {
            StorageFolder picfolder = KnownFolders.PicturesLibrary;
            IReadOnlyList<StorageFile> list = await picfolder.GetFilesAsync();
            string picinfo = "图片库文件夹下文件名是:";
            foreach (StorageFile item in list)
            {
                picinfo += item.Name + "+";
            }
            tb_show.Text = picinfo;
        }

        private async void btnCreateFolder_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                StorageFolder picfolder = KnownFolders.PicturesLibrary;
                await picfolder.CreateFolderAsync("NewMusic", CreationCollisionOption.ReplaceExisting);
                tb_show.Text = "提示:“库\\图片\\”文件夹下新建“库\\图片\\NewMusic”文件夹";
            }
            catch (Exception ex)
            {
                tb_show.Text = "提示:未找到该文件夹,请先创建文件夹";
            }
        }

        private async void btnDeleteFolder_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                StorageFolder picfolder = KnownFolders.PicturesLibrary;
                var newFolder = await picfolder.GetFolderAsync("NewMusic");
                await newFolder.DeleteAsync();
                tb_show.Text = "提示:“库\\图片\\”文件夹下删除“库\\图片\\NewMusic”文件夹";
            }
            catch (Exception ex)
            {
                tb_show.Text = "提示:未找到该文件夹,请先创建文件夹";
            }
        }

        private async void btnReNameFolder_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                StorageFolder picfolder = KnownFolders.PicturesLibrary;
                var newFolder = await picfolder.GetFolderAsync("NewMusic");
                await newFolder.RenameAsync("New Picture");
                tb_show.Text = "提示:“库\\图片\\”文件夹下重命名“库\\图片\\NewMusic”文件夹";
            }
            catch (Exception ex)
            {
                tb_show.Text = "提示:未找到该文件夹,请先创建文件夹";
            }
        }
复制代码

  最后如需源码请点击 Win8File2.rar 下载



本文转自程兴亮博客园博客,原文链接:http://www.cnblogs.com/chengxingliang/archive/2012/12/24/2829820.html,如需转载请自行联系原作者

相关文章
|
14天前
|
C# Windows
.NET开源免费的Windows快速文件搜索和应用程序启动器
今天大姚给大家分享一款.NET开源(MIT License)、免费、功能强大的Windows快速文件搜索和应用程序启动器:Flow Launcher。
|
1月前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
1月前
|
网络协议 数据安全/隐私保护 Windows
Windows Server 各版本搭建域控制器实现通过域管理用户(03~19)
Windows Server 各版本搭建域控制器实现通过域管理用户(03~19)
45 1
|
1月前
|
存储 缓存 数据安全/隐私保护
Windows 命令提示符(CMD)操作(二):系统信息和管理
Windows 命令提示符(CMD)操作(二):系统信息和管理
43 0
|
1月前
|
存储 安全 数据安全/隐私保护
Windows部署WebDAV服务并映射到本地盘符实现公网访问本地存储文件
Windows部署WebDAV服务并映射到本地盘符实现公网访问本地存储文件
265 0
|
2月前
|
存储 安全 Shell
windows 操作系统隐藏文件夹 .ssh 的作用
windows 操作系统隐藏文件夹 .ssh 的作用
52 0
|
30天前
|
开发框架 数据安全/隐私保护 开发者
HBuilder开发者必备!Windows上传IPA文件的软件分享
HBuilder开发者必备!Windows上传IPA文件的软件分享
21 1
|
2月前
|
存储 安全 Shell
windows 系统 c 盘 .ssh 文件夹里的 known_hosts 文件的作用
windows 系统 c 盘 .ssh 文件夹里的 known_hosts 文件的作用
68 0
|
2月前
|
安全 Shell 网络安全
windows 系统 c 盘 .ssh 文件夹里的 id_rsa 文件的作用
windows 系统 c 盘 .ssh 文件夹里的 id_rsa 文件的作用
38 0
|
3天前
|
网络协议 安全 测试技术
Windows安装禅道系统结合Cpolar实现公网访问内网BUG管理服务
Windows安装禅道系统结合Cpolar实现公网访问内网BUG管理服务