问题
I want to check if the file inside my device exist. When the variable crphoto1 is empty or the file does not exist the "Photo1" json should be {"Photo1", ""}
JObject ph1json = string.IsNullOrEmpty(crphoto1)
? new JObject
{
{"ContactID", crcontactID},
{"Photo1", ""}
}
: new JObject
{
{"ContactID", crcontactID},
{"Photo1", File.ReadAllBytes(crphoto1)}
};
回答1:
If you are just looking for how to check if file exist you can use
using System.IO;
string fileName = Path.Combine(Environment
.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "yourfile.jpg");
JObject ph1json;
bool doesExist = File.Exists(fileName);
if (!doesExist || string.IsNullOrEmpty(crphoto1))
{
ph1json = new JObject
{
{"ContactID",crcontactID},
{ "Photo1",""}
};
}
else
{
ph1json = new JObject
{
{"ContactID",crcontactID},
{"Photo1",File.ReadAllBytes(crphoto1)}
};
}
来源:https://stackoverflow.com/questions/54663101/how-to-check-if-the-file-exist-in-xamarin-forms