1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

the process cannot access the file because it is being used by another process C#

Discussion in 'C, C++, C#' started by MisterNick, Nov 6, 2014.

  1. MisterNick

    MisterNick Registered Member

    Joined:
    Oct 22, 2014
    Messages:
    54
    Likes Received:
    45
    Occupation:
    Programmer
    Location:
    Tbilisi,Georgia
    Hello guys,

    I'm trying to create a file if it's not exists and then write some string to it.But i'm getting an error message "the process cannot access the file because it is being used by another process".

    if (File.Exists(file))
    {
    }
    else { File.Create(file);

    File.SetAttributes(file, FileAttributes.Hidden);
    using (StreamWriter writer = new StreamWriter(file, true))
    {
    writer.Write("3600");
    writer.Close();
    }
    }

    Once program will create the file now it's time to write something to the file.And when program is on writing function it's showing an error message
     
  2. Avocado Plugins

    Avocado Plugins Junior Member Premium Member

    Joined:
    Jun 23, 2014
    Messages:
    108
    Likes Received:
    50
    Occupation:
    Software Developer & Entrepreneur
    Location:
    Boston
    Home Page:
    First of all how about you revert your logic in say !File.Exists(file) so that you don't have to have that werid empty block (unless you were meant to put something there then it is understandable).

    Second assign the File.Create(file) to a variable so you can close and dispose the instance of that down below.
     
  3. Xp3r7

    Xp3r7 Regular Member

    Joined:
    Sep 9, 2011
    Messages:
    286
    Likes Received:
    386
    Occupation:
    Freelancer
    Location:
    In the middle of nowhere
    Try to set the attribute to hidden after the writing logic.

     
  4. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    614
    Likes Received:
    313
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    whats wrong with
    Code:
    
    File.WriteAllText(filepath, contents);
    
    also your code is wrong

    Code:
    using (StreamWriter writer = new StreamWriter(file, true))
    {
      writer.Write("3600");
      writer.Close();
    }
    
    writer.close is not need as you have the using statement, doubt that is causing the issue, but...



    Another way would be to use a lock, have a static object that you can assign a lock to, then any time you want to access the file, lock it, if the file is in use, the lock will pause the execution of the code

    Code:
    static object locker = new object();
    
    .
    ..
    ...
    ...
    
    lock(locker){
      //all the code you want to do on the file
    }
    
    
    
    
     
  5. scharp

    scharp Newbie

    Joined:
    Mar 2, 2015
    Messages:
    1
    Likes Received:
    0
    Your problem is this:

    Code:
    if (File.Exists(file))
                {
                }
                else { File.Create(file); }
    change it to this
    Code:
    if (File.Exists(file))
                {
                }
                else { File.Create(file).Close(); }
     
  6. Chdead

    Chdead Newbie

    Joined:
    Apr 12, 2015
    Messages:
    6
    Likes Received:
    0
    this is the correct code :)
    if (File.Exists(file)) {
    using (StreamWriter writer = new StreamWriter(file, true))
    {
    writer.Write("3600");
    }
    else {
    Console.WriteLine("{0} does not exists", file);
    }
     
  7. ekapek

    ekapek Jr. VIP Jr. VIP Premium Member

    Joined:
    Aug 2, 2010
    Messages:
    245
    Likes Received:
    41
    Home Page:
    Chdead not really - if file doesn't exist it doesn't create it
     
  8. bitm0de

    bitm0de Newbie

    Joined:
    Jun 13, 2015
    Messages:
    12
    Likes Received:
    1
    What are you talking about? The StreamWriter instance will create the file...
     
  9. itz_styx

    itz_styx Jr. VIP Jr. VIP

    Joined:
    May 8, 2012
    Messages:
    188
    Likes Received:
    33
    Occupation:
    CEO / Admin / Developer
    Location:
    /dev/mem
    Home Page:
    try
    Code:
    var enc = Encoding.ASCII;
    if (File.Exists(file))
    {
       writer = new StreamWriter(new FileStream(file, FileMode.Append, FileAccess.Write, FileShare.None, 4096, FileOptions.None), enc);
    }
    else
    {
       writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.ReadWrite), enc);
    }