This doesn't seem to make sense, so I'm obviously doing something wrong:
DirectoryInfo folder = new DirectoryInfo(Environment.CurrentDirectory + @"\Test");
if (folder.Exists == false) {
folder.Create();
var doesItExists = folder.Exists;
}
Creates a folder if it doesn't exist. Except doesItExists
is always false. Why would it be false if I just created it?
mkdir WILL give you an error if the directory already exists. mkdir -p WILL NOT give you an error if the directory already exists. Also, the directory will remain untouched i.e. the contents are preserved as they were.
const fs = require("fs") fs. access("./directory-name", function(error) { if (error) { console. log("Directory does not exist.") } else { console. log("Directory exists.") } })
createDirectories() creates a new directory and parent directories that do not exist. This method does not throw an exception if the directory already exists. What happens if permission is required to create ? For Android It only works on API 26 and up so Make sure to check this line if (Build.
The value in folder.Exists
is cached. I would suggest doing this check:
var doesItExists = Directory.Exists(folder.FullName);
Or you could call folder.Refresh()
to update the cache before checking if the directory exists after creating it. See this previous answer.
Assuming that folder
is a DirectoryInfo
or FileSystemInfo
, it reads its values once, and then returns cached values. It doesn't notice that you've created the directory. Call Refresh()
.
Alternatively use Directory.Exists()
.
this will get you true,you need to call refresh():
DirectoryInfo folder = new DirectoryInfo(Environment.CurrentDirectory + @"\Test");
if (folder.Exists == false)
{
folder.Create();
folder.Refresh();
var doesItExists = folder.Exists;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With