File and folder actions
In this article we will learn about the files in Win 8 and the various actions of the folders.
File actions in this article are mainly about: deleting files/moving files/copying files/renaming files
Folder operation is divided into: Read folder/Create Folder/delete Folder/Rename folder
First, all of the XAML code file sections are posted:
<grid background= "{StaticResource Applicationpagebackgroundthemebrush}" > <!--display area--> <tex Tblock horizontalalignment= "left" margin= "137,42,0,0 textwrapping=" Wrap "text=" FileName: "VerticalAlignment
= "Top" height= "width="/> <textbox horizontalalignment= "left" margin= "185,33,0,0" textwrapping= "Wrap" text= "Test.txt" verticalalignment= "Top" width= "121" name= "Tbfilename"/> <textbox Horizontal Alignment= "left" margin= "457,33,0,0" textwrapping= "Wrap" text= "The contents of the file to be added by default" verticalalignment= "Top" Width = "431" name= "tbcontent"/> <textblock horizontalalignment= "left" margin= "396,42,0,0" textwrapping= "Wrap" Text = "File content:" verticalalignment= "Top" height= "width="/> <textblock horizontalalignment= "Left" margin= "127,163,0,0" textwrapping= "Wrap" text= "Hint:" verticalalignment= "Top" height= "" width= "7
"Name=" Tb_show "/>" <!--Delete file Move file Copy file Rename file--> <button content= "Create and write to file horizontalalignment=" left "margin=" 127,99,0 , 0 "name=" btncreatefile "verticalalignment=" Top "click=" Btncreatefile_click "/> <button, Conte nt= "read string file" horizontalalignment= "left" margin= "757,99,0,0" x:name= "Btnreadfile" verticalalignment= "Top" Cl
ick= "Btnreadfile_click"/> <button content= "Delete file" horizontalalignment= "left" margin= "127,223,0,0" X:name= "Btndeletefile" verticalalignment= "Top" click= "Btndeletefile_click"/> <button "moving Files" Content= Talalignment= "left" margin= "320,223,0,0" x:name= "Btnmovefile" "verticalalignment=" Top "click=" Btnmovefile_click "/> <button content=" Copy files horizontalalignment= "left" margin= "560,223,0,0" x:name= "Btncopyfile" Verticalalignment= "Top" click= "Btncopyfile_click"/> <button content= "Rename file" horizontalalignment= "left" Margi
N= "780,223,0,0" X:name= "Btnrenamefile" verticalalignment= "Top" click= "Btnrenamefile_click"/> <!--Read folder Create folder Delete folder rename file Clip--> <button content= "read folder" horizontalalignment= "left" margin= "127,296,0,0" Verticalalignme Nt= "Top" name= "Readfolder" click= "Readfolder_click"/> <button content= "Create folder" Horizontalalignment= "left" Mar
Gin= "305,296,0,0" verticalalignment= "Top" x:name= "Btncreatefolder" click= "Btncreatefolder_click"/> <button content= "Delete folder" horizontalalignment= "left" margin= "545,296,0,0" verticalalignment= "Top" x:n Ame= "Btndeletefolder" click= "Btndeletefolder_click"/> <button content= "Rename folder" Horizontalalignment= "left" Ma
Rgin= "766,296,0,0" verticalalignment= "Top" x:name= "Btnrenamefolder" click= "Btnrenamefolder_click"/> </Grid>
Second, let's take a look at the CS code for deleting files/moving files/copying files/renaming files:
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 = "Hint:" + this.tbFileName.Text.Trim () + "file Delete successful!";
}
catch (Exception ex)
{
Tb_show. Text = "Hint: The file was not found, please create the file first";
}
}
Private async void Btnmovefile_click (object sender, RoutedEventArgs e)
{
Try
{
Move a file from a document to a music library
Storagefile SF = await Storagefolder.getfileasync (This.tbFileName.Text.Trim ());
Storagefolder NewFolder = knownfolders.musiclibrary;
Await SF. Moveasync (NewFolder, "MoveFile.txt", namecollisionoption.replaceexisting);
Tb_show. Text = "Hint: library \ \ document \ \" + This.tbFileName.Text.Trim () + "" file moved to library \ Music \\moveFile.txt "";
}
catch (Exception ex)
{
Tb_show. Text = "Hint: The file was not found, please create the file first";
}
}
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 = "Hint: library \ \ document \ \" + This.tbFileName.Text.Trim () + "" File copy to "library \ Document \\copyFile.txt";
}
catch (Exception ex)
{
Tb_show. Text = "Hint: The file was not found, please create the file first";
}
}
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 = "Hint: library \ \ document \ \" + This.tbFileName.Text.Trim () + "" file renamed to library \ Document \\renameFile.txt "";
}
catch (Exception ex)
{
Tb_show. Text = "Hint: The file was not found, please create the file first";
}
}