.net - C#: Check if a file is not locked and writable -
I do not want to write files or use the list of files before starting to replace them. Sure I know that the time of file check and file copy is an opportunity to lock one or more files to another person, but I handle those exceptions. I would like to run this test before copying the file, because the complete list of files is a good chance to succeed, if one file fails to fail in the middle of the operation.
Any one of you or an indication in the right direction
This thing There is no guarantee that you have a similar list, it is going to be in the form of the next one, because when someone comes back to you you can control the file.
I do however, in a way - see "lock" those files that you want to replace by receiving their respective filestream objects. In this way you are certain that you have locked all the "available" files and then you can replace them the way you want them.
Public Zero TestGivenFiles (list & lt; string & gt; listFiles) {list & lt; File stream & gt; ChangeableFileStream = GetFileStreams (ListFiles); Console.light line ("received files" = substituted ablabel file stream.); Foreach (replace the filestream file in thephilstream stream) {// Change the way files you want fileStream.Close (); }} Public listing & lt; Filestream & gt; GetFileStreams (list & lt; string & gt; list files again) {List & lt; File stream & gt; Changed files = new list & lt; File stream & gt; (); Foreach (location in string sFileLocationFilesToReplace) {FileAttributes fileAttributes = File.GetAttributes (sFileLocation); If ((file attributes and file entries. Readonly)! = File entries, read only) {// Make sure the file is not read-only {filestream currentWriteableFile = File.OpenWrite (sFileLocation); ReplaceableFiles.Add (currentWriteableFile); } Could not get the stream for the {Console.WriteLine ("" + sFileLocation + "'is probably in use");}}} replacement changed files;}
that did not receive the stream Said, you are trying to change them one by one and you can ignore those people who you can not.
Comments
Post a Comment