- Home /
XmlWriter.Create exception, "Could not find the file..." on Android
Hi all,
I have some C# code which is throwing the above exception when I call XmlWriter.Create. I've tested on Windows and Android and have only seen this happen on the latter.
My first thought was that the directory didn't exist so I added a Directory.Create call above it. No dice.
Next I thought the file, if it already exists, might somehow be in use. To try and catch this I added a File.delete call expecting that to generate a similar exception before my call to XmlWriter.Create. Again, no joy.
I also tried placing the call to XmlWriter.Create in a loop so that it keeps trying till it succeeds. I found that if the call fails (it doesn't always) it only fails once then works on the second go round the loop.
Here's broadly what the code looks like;
string path = GetFilePathWithExtension();
string dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.Delete(path);
for (int i = 0; i < 10; ++i)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.CloseOutput = true;
try
{
using (XmlWriter writer = XmlWriter.Create(path, settings)) // Exception thrown here
{
Save(writer);
IsDirty = false;
}
break;
}
catch (Exception e)
{
this.LogError(e.Message);
}
}
And here's the text of the exception;
"Could not find file \"/storage/emulated/0/Android/data/com.guycorbett.theadventureengine/files/libraries/local/official.libraryinfo\""
I had been tidying some code and now this is no longer happening. Looking at my diffs the only thing I actually changed was that directly before this function was called, the calling code tried to delete the file. It could just be coincidence and that the issue isn't actually fixed, as it was only intermittent before. If that has fixed it I've certainly no idea why. So if anyone as any clue as to what it might be, please let me know.