0

I am trying to create a .json file using the C# create functions, but it doesn't want to delete the old file

try
{
    if (File.Exists(filePath))
    {
        File.Delete(filePath);
    }

    using (FileStream fs = File.Create(filePath))
    {
         Byte[] info = new UTF8Encoding(true).GetBytes(result);
         fs.Write(info, 0, info.Length);
    }
}

catch (Exception ex)
{
    LotNumberSearchResult.Text = ex.ToString() + " ---Error while writing";
}

The catch doesn't return anything either.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • sounds obvious, but is filePath definitely correct? – Jim Jeffries Aug 27 '15 at 09:33
  • Do you [have permission](http://stackoverflow.com/questions/265953/how-can-you-easily-check-if-access-is-denied-for-a-file-in-net) to write / delete from `filePath`? I tried your code and it is working as expected... – Jan 'splite' K. Aug 27 '15 at 09:35
  • Yes, I double checked the filepath and it is 100% correct – FullyHumanProgrammer Aug 27 '15 at 09:35
  • I had my code create a new (different) file, and that works, It is really the delete part not working... Does deleting require different permission? – FullyHumanProgrammer Aug 27 '15 at 09:38
  • yes, deleting is under "Full permission" || "Execute" ( [source](https://msdn.microsoft.com/en-us/library/bb727008.aspx) ), "Read" & "Write" is not enough – Jan 'splite' K. Aug 27 '15 at 09:47
  • When you say "It is really the delete part not working" appears strange because if you try to call File.Create with an existing file, it should throw an exception! – Caverna Aug 27 '15 at 09:50
  • @Jan 'splite' Kondelík Path.GetTempFileName() returns a filename, and running as administrator does not change the outcome either... – FullyHumanProgrammer Aug 27 '15 at 09:52
  • @Caverna Unfortunately, it does not, if I got an exeption back that would make this a lot easier... – FullyHumanProgrammer Aug 27 '15 at 09:54
  • @FullyHumanProgrammer So it is a real mystery :( Like you, I would expect exception when IO dont have permission... Are you using Threads? (btw, `Path.GetTempFileName()` should have all permissions granted, depends what do you want to do with your file) – Jan 'splite' K. Aug 27 '15 at 10:02
  • @Jan'splite'Kondelík I am not using any threads and I'm running the program with administrator permissions. – FullyHumanProgrammer Aug 27 '15 at 10:07

2 Answers2

1

After some extra testing I found out that the problem was a slow webserver. The webserver didn't have enough time to send the updated file information through, giving it a few minutes and then trying to get the information from the file again showed that the file was indeed deleted and that the information inside was updated on creation of the new file

0

There're different possibilities:

  • Wrong URL
  • You need the permission for the Save/Delete
  • You're code is working for me so I think it's not a script problem

You can also try to save a file in the same path of the old one, if it change you can use this method...