title: How do i check if file is in use/busy before continue the rest of the code?
body: <p>I have this part:</p>
  
  <pre><code>public string SendResponse(HttpListenerRequest request)
          {
              string result = "";
              string key = request.QueryString.GetKey(0);
              if (key == "cmd")
              {
                  if (request.QueryString[0] == "uploadstatus")
                  {
                      switch (Youtube_Uploader.uploadstatus)
                      {
                          case "uploading file":
                              return "uploading " + Youtube_Uploader.fileuploadpercentages;
  
                          case "status":
                              return Youtube_Uploader.fileuploadpercentages.ToString();
  
                          case "file uploaded successfully":
                              Youtube_Uploader.uploadstatus = "";
                              return "upload completed," + Youtube_Uploader.fileuploadpercentages + ","
                                 + Youtube_Uploader.time;
                          default:
                              return "upload unknown state";
                      }
  
  
                  }
                  if (request.QueryString[0] == "nothing")
                  {
                      return "Connection Success";
                  }
                  if (request.QueryString[0] == "start")
                  {
                      StartRecrod();
                      result = "Recording started";
                  }
  
                  if (request.QueryString[0] == "stop")
                  {
                      dirchanged = false;
                      StartRecrod();
                      result = "Recording stopped and preparing the file to be shared on youtube";
                      string fileforupload = await WatchDirectory();
                      await WaitForUnlockedFile(fileforupload);
                      using (StreamWriter w = new StreamWriter(userVideosDirectory + "\\UploadedVideoFiles.txt", true))
                      {
                          w.WriteLine(fileforupload);
                      }
                      uploadedFilesList.Add(fileforupload);
                      Youtube_Uploader youtubeupload = new Youtube_Uploader(uploadedFilesList[0]);
                  }
              }
              else
              {
                  result = "Nothing have been done";
              }
  
              return result;
  
          }
  </code></pre>
  
  <p>Then i have the WatchDirectory method:</p>
  
  <pre><code>FileSystemWatcher watcher;
  
  private void WatchDirectory()
  {
      watcher = new FileSystemWatcher();
      watcher.Path = userVideosDirectory;
      watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
      watcher.Filter = "*.mp4";
      watcher.Changed += new FileSystemEventHandler(OnChanged);
      watcher.EnableRaisingEvents = true;
  }
  </code></pre>
  
  <p>The OnChanged event:</p>
  
  <pre><code>private void OnChanged(object source, FileSystemEventArgs e)
  {
      var info = new FileInfo(e.FullPath);
      fileforupload = info.FullName;
      while(IsFileLocked(fileforupload) == true)
      {
          System.Threading.Thread.Sleep(100);
      }
  }
  </code></pre>
  
  <p>Then the IsFileLocked method:</p>
  
  <pre><code>public bool IsFileLocked(string filename)
  {
      bool Locked = false;
      try
      {
          FileStream fs =
                      File.Open(filename, FileMode.OpenOrCreate,
                      FileAccess.ReadWrite, FileShare.None);
          fs.Close();
      }
      catch (IOException ex)
      {
          Locked = true;
      }
      return Locked;
  }
  </code></pre>
  
  <p>Now the order should be that first it will go to the WatchDirectory and then in the while loop will check if the file is locked/busy once the file is not locked/busy any more continue with the rest of the code the StreamWriter the uploadedFilesList.Add and the Youtube_Uploader...</p>
  
  <p>First i'm not sure if it's the right way to use the While loop with the 100.
  And second how do i make that it will first finish the file lock checking before continue ? Now what it does is getting to the WatchDirectory then making the StreamWriter... Not the order i want it to be.</p>