Windows 8技巧:windows 8文件 文件夹管理[文件以及文件夹操作]
在本文中我们将学习win 8中的文件以及文件夹的各种操作。
在本文中文件操作主要是讲述:删除文件/移动文件/复制文件/重命名文件
文件夹操作分为:读取文件夹/创建文件夹/删除文件夹/重命名文件夹
首先贴出所有的Xaml代码文件部分:
复制代码代码如下:
Name="btnCreateFile" VerticalAlignment="Top" Click="btnCreateFile_Click"/>
x:Name="btnReadFile" VerticalAlignment="Top" Click="btnReadFile_Click"/>
x:Name="btnDeleteFile" VerticalAlignment="Top" Click="btnDeleteFile_Click"/>
x:Name="btnMoveFile" VerticalAlignment="Top" Click="btnMoveFile_Click"/>
x:Name="btnCopyFile" VerticalAlignment="Top" Click="btnCopyFile_Click"/>
x:Name="btnReNameFile" VerticalAlignment="Top" Click="btnReNameFile_Click"/>
VerticalAlignment="Top" Name="readFolder" Click="readFolder_Click"/>
VerticalAlignment="Top" x:Name="btnCreateFolder" Click="btnCreateFolder_Click"/>
VerticalAlignment="Top" x:Name="btnDeleteFolder" Click="btnDeleteFolder_Click"/>
VerticalAlignment="Top" x:Name="btnReNameFolder" Click="btnReNameFolder_Click"/>
其次我们来看删除文件/移动文件/复制文件/重命名文件的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
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_jb51net.rar 下载
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
本文地址:/jtjc/win10/167832.html