Lynnstrum
Lynnstrum

Reputation: 194

File is created once, then stops

I am trying to read from a folder, and delete a specified file from inside. I am able to do this one time without any issues. However after doing it the first time, I am no longer able to Create the new file. The old one is still deleted, but the new file is no longer being created as it should. My question is; From the code provided why would the task work once, but not after that? It's being deleted after the first time but won't re-create.

EDIT: The issue was in permissions. I changed the folder security settings to allow reading/writing and I am now able to do it as I wanted. However, I need it to automatically set the security settings if possible as other users may not know how to do so.

if (Directory.Exists(path1))
{
    if (File.Exists(path1 + "\\launchinfo.txt"))
    {
        File.Delete(path1 + "\\launchinfo.txt");
        using (FileStream fs = File.Create(path1 + "\\launchinfo.txt"))
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("[Connection]\n" + Form1.ipaddress + "\nport=0000\nclient_port=0\n[Details]\n" + Form1.playername);
            fs.Write(info, 0, info.Length);
        }
    }
    else
    {
        using (FileStream fs = File.Create(path1 + "\\launchinfo.txt"))
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("[Connection]\n" + Form1.ipaddress + "\nport=0000\nclient_port=0\n[Details]\n" + Form1.playername);
            fs.Write(info, 0, info.Length);
        }
    }
}

Upvotes: 1

Views: 83

Answers (1)

Darren Wainwright
Darren Wainwright

Reputation: 30747

You've not posted any possible exception you may be running into - if you do have one, please post it.

That being said, it's possible that you're encountering File in use by another process when trying to delete the file - especially if you're calling your function moments after creating a file.

A method to get around this is to check if a process is using the file before you try to delete it.

string fullPath = Path.Combine(path1, "launchinfo.txt");
if (Directory.Exists(path1))
 {
      if (File.Exists(fullPath))
      {  
         // Call a method to check if the file is in use.         
         if (IsFileLocked(new FileInfo(fullPath)){
            // do something else because you can't delete the file
         } else {
             File.Delete(fullPath);
          }
      }

      using (FileStream fs = File.Create(fullPath))
      {
         Byte[] info = new UTF8Encoding(true).GetBytes("[Connection]\n" + Form1.ipaddress + "\nport=0000\nclient_port=0\n[Details]\n" + Form1.playername);
         fs.Write(info, 0, info.Length);
       }
 }

A method to check if the file is in use by another process

 protected virtual bool IsFileLocked(FileInfo file)
    {
        FileStream stream = null;

        try
        {
            stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
        }
        catch (IOException)
        {
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }    
        //file is not locked
        return false;
    }

Upvotes: 2

Related Questions