Utilisation de la classe de fichiers C # de l’espace de noms System.IO Ajouter, copier, déplacer et remplacer le contenu [.NET 6]




Dans cette vidéo, je vais expliquer comment ajouter le contenu d’un fichier à l’aide de la classe File de l’espace de noms System.IO. Deuxièmement, je vais montrer …

Hello everyone and welcome to .Net Core Central  in today’s video I will continue my discussion   of file class from system dot ion namespace  if you have not watched my previous video I   strongly suggest you go and watch it as I  covered few of the concepts in that video  

I’m going to share the URL of the video  in the description so what we are going   to cover today in today’s video fast I’ll start  with how to append item into an existing file   and then I’m going to show how to copy a file  from one location to a different location  

Next how to move a file and finally I’ll finish  the video with how to replace content of an   existing file so these are the four things that  I’m going to cover as a part of the video as in  

From the last video I had a folder called C:temp  where I had a test.txt file and the text.txt file   right now has a content of test content that’s  about it so first we are going to start with how  

To append content to an existing file and the  existing file is the file which I just showed   you which is the test.txt so for appending item  to a file we can use few variation of append   as you can see we have AppendAllLines or  AppendAllLinesAsync which is the function  

Which appends line to a file and then closes  the file now if the file specified does not   exist this method is going to create a file  write the content of the file and then close   the file that’s what it essentially going to  do I’m going to show you both the scenarios one  

Scenario where we have the file another scenario  we do not have the file then the next one what   we can use is AppendAllText AppendAllText or  AppendAllTextAsync both of them or one of them   where for all the example if there is an async  method available we are going to go ahead and  

Show the async implementation because that’s what  we will be using in most of the real life scenario   now for AappendAllText scenario it  appends the specified string to a file   and then it just closes the file and if the  file does not exist then it’s going to go  

Ahead and create the file it’s the same thing as  AppendAllLines it’s just it’s dealing with a text   and AppendText is something which essentially  just creates a file if the file does not exist   or it returns an existing file as a file  stream writer so it’s going to return a  

StreamWriter and then using the StreamWriter  we can write into file now if we deal with   StreamWriter we have to take care of closing  the StreamWriter and things like that so in most   cases AppendAllTest and AppendAllLines itself  is going to cover all our cases so let’s first  

Start with AppendAllLlinesAsync and I’ll just add  a await and after specifying the file path we have   to specify the content for content we can just  create a new string it can be fast line and then   second line it’s about it so right now the file  which is test.txt already exists in the temp  

Folder so it is just going to append and as I  showed before the test.txt has a test content   so it’s going to add this content after the test  content so let’s go ahead and run this program and now the program is completed running  so I’ll go ahead and open the text file  

And in the text file we can see  test content than the first line   and then the second line as expected now  let me go ahead and just delete this file so I’m just going to delete this file  and I’m going to run the program again

And this time if I go and open this file I’m  just going to see the first line and second   line and as I can see that the file is created  because the file did not exist before so it  

Went ahead and just created the file so that’s  the AppendAllLines and then we can similarly AppendAllText and in this case, if  just one string and here we can say   third line which will be added to the existing  file and I’m going to run this and for this one  

Also if the file doesn’t exist it behaves the same  way it will just create a new file and add the   text so I’m not going to show that because it’s  redundant it’s the same thing and you can see here  

First line second line and it added the third line  so that’s about how to append item now let’s check   how do we copy so for copy again we can use the  copy method so you can use copy and for copy you  

Can see it’s the source file and the destination  file so we have the source file and for copy we do not have an async copy is just  a synchronous method and it copies the   existing file to a new file now what copy does  also it will override a file with the same name  

Only if we specify a Boolean value and if  the Boolean value is not specified then it   is not going to override so first let’s not  worry about overwrite first let’s just go   ahead and just copy it into a new location  and for that what we can do is we can just  

Keep the same location just a  different file name so we can say   this can we testcopy that’s about it and we have  the test file the test.txt already so let’s run   this and as soon as it complete running we should  be able to see a testcopy file here and it’s an  

Identical copy of the existing item now let’s do  one thing let’s just change this and here it’s a   new data so we change the data of test copy now if  we go and if we try to run this this time what is  

Going to happen is we are going to get an error  it says this file already exists so you cannot   copy into this file but as I mentioned there is  a third parameter which says bool override if by  

Default this value is of course false otherwise  it would have worked so now if we pass it here   as true that means we want it to override if the  file exists and we know the file exist so what  

Is going to happen is the content of this file  which is right now new data will be replaced by   content of this file which is fast second and  third line so let’s run this application now

And now if you go here we’re going to test  copy we can see the content of this file   has been replaced by fast second and third  line as expected so that is all about copy   it has two variation one is without override  in which case if the file exists is going to  

Throw an error and one is with the Boolean  value override and if we set it as true   it is just going to override the file so  let’s try returning setting up false here   let’s see what happens when we set the false  instead of true and run the application  

We get the same behavior as not passing  anything which is the System.IO.IOException   now that we covered copy let’s go ahead and cover  Move which is the third function we wanted to   cover as a part of this video so move again move  has also two variation one with the Boolean value  

Of override and one without what move does is  it moves a specified file when to a new location   providing an option to specify a new file  name so you can specify a new file name or   you can keep the same file name and  with the Boolean option also if the  

File with the same name exists in the same  location or the destination location it is   going to overwrite that file just like  copied it so if we have to try out move   so as you can see move also have two variation one  is Source destination and second one is you know  

Do you want to overwrite now we cannot move  from first let me delete the test copy file and let’s move test into testcopy and let’s  get rid of this variation for the time being we   don’t need that and let’s run the application  and now if we go here we’ll see that test has  

Been replaced by testcopy because it is just  moving since we did not change the folder it   did not do anything and the content of the file  will remain same but this example is not so  

Interesting so let me create a new folder inside  of the temp folder so let me go ahead create a   new folder and say it is internal and then what we  can do is here we can say tampinternaltestcopy

And run this application now what is going to  happen is the test is going to be moved from   temp into the internal folder of temp so now  we don’t see the test here and if we go inside  

Test is moved here with the name of testcopy now  let me do one more thing now let me just copy it   back here rename it as test we still have the  testcopy inside and let’s change the content  

Of the testcopy as test copy okay and then  let’s go here first let’s try to do this we   should see an error because the testcopy already  exists so cannot create a file because you know   the file already exists now if we pass  that Boolean value and we pass it as true  

We know that if we pass as false we are going to  get this exact same error because it’s the same   behavior now if we run this application now what  is going to happen is it is going to move the test  

So we don’t see test anymore it is move  test into this testcopy and it would have   override the content of this file and we  can see that it has overridden the content   of this file first line Second Line third  line as expected now let’s copy it back  

And let’s paste it here and let me rename the  file to test let’s go back here so this is two   variation of move and now we are going to talk  about Replace so this is the fourth method that  

We wanted to cover today of the System.IO.File  class or the File class from System.IO namespace   so what does Replace do the Replace method  replaces the content of a specified file   with the content of another file deleting the  original file and then creating a backup of the  

Replaced file that’s what it does so what it  is going to do is if we are replacing testcopy   with test it is going to delete test and then it  will put the content of the test into testcopy  

That’s all it is going to do it is very similar  to what Move does so now if we try Replace here   as you can see replace has three parameter  source file destination file and the destination  

Backup file name which is nullable so we’re just  going to leave it at now we don’t need to have   a backup and if let me just change  the content of the testcopy to replaced content, as the content of this file,  will be replaced so now if we run this application

And we go here we can see the content of this file  is changed it has the content of test.txt and the   test.txt is deleted which is the original file  and Replace also has another overload which has   a Boolean is called ignoreMetadataError  it’s true to ignore merge errors  

Such as attribute and access control list  from the replaced file to the replacement file   otherwise false so this is something I don’t think  we’re going to use ever especially when we are   dealing with files in a Docker container some of  these things are not even useful or meaningful so  

These are the four methods that I wanted to cover  as a part of today’s video If you like this video   please give me a thumbs up if you are new to my  channel and you think you are getting value out  

Of my Channel please subscribe to my channel  and thanks so much for watching this video