Hello my dear developers!
How do you feel today?
These days I’ve been reviewing some code involving JSON and file managing, as always you are supposed to know the 101 on how to work with files, storing, opening, editing, etc.
But the tricky thing always comes when you want to make sure that a file exists and you get false positives and everything starts throwing weird exceptions that you didn’t expect. You at that point start spending your precious time chasing ghost’s through a hundred break points, stepping in to execution and such.
“Good if I could remember… I’ve had this one before”, I thought. There’s some option called open create or something (FileMode.OpenOrCreate) but now I’m using static methods from File Class such as WriteAllText() or ReadAllText() and those don’t take this options. Da@# it!
So after some thinking and a lot of rushĀ I went in to “I don’tĀ give a crap about technical debt, I just wanna finish mode” and started writing the following code every time I wanted to get a file from storage either to read o write on it.
var fs = File.Open(path, FileMode.OpenOrCreate);
fs.Close();
“God this is crappy code I can’t believe I did it again, now if this doesn’t work I’ll have to go fixing it all over the project”, IĀ felt.
Thereafter my guilt grew unbearable so I started developing a much neater code that centralizes everything on a method called GetPath, that has a bool createIfNotExists that when set to true it executes the code shown before and else you can do things however you like regarding how to ensure file existence.
public string GetPath(string fileName, string fileExtension, bool createIfNotExists = false, string folderPath = "")
{
if(string.IsNullOrWhiteSpace(_basePath))
{
throw new IOException("File Manager needs a basePath to work with");
}
string path = null;
if (!string.IsNullOrWhiteSpace(fileName) && !string.IsNullOrWhiteSpace(fileExtension))
{
if (fileExtension.Contains("."))
{
fileExtension = fileExtension.Replace(".", "");
}
if (string.IsNullOrWhiteSpace(folderPath))
{
folderPath = "";
}
else if (!folderPath.EndsWith(@"\"))
{
folderPath += @"\";
}
//this ensures directory creation
if (!string.IsNullOrWhiteSpace(folderPath) && !Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
var baseAndFolder = string.Format(@"{0}\{1}", _basePath, folderPath);
if(!baseAndFolder.EndsWith(@"\"))
{
baseAndFolder += @"\";
}
baseAndFolder= baseAndFolder.Replace(@"\\", @"\");
path = string.Format(@"{0}{1}.{2}", baseAndFolder, fileName, fileExtension);
path = path.Replace(@"\\", @"\");
if (createIfNotExists)
{
//this ensures file creation
var fs = File.Open(path, FileMode.OpenOrCreate);
fs.Close();
}
}
return path;
}
If you’d like to have a pick at the complete project you have it here it’s the Things that I forget when I sneeze project evolved.
I know that this is not the most optimal solution so I encourage everybody that comes with a better one to leave a comment on how to improve it in any way.
Happy coding and remember…there is life beyond codingš
Did you like it? Share this: