我想检查设备内部的文件是否存在。当变量 crphoto1 为空或文件不存在时,“ Photo1 ” json应为{"Photo1", ""}
JObject ph1json = string.IsNullOrEmpty(crphoto1)
? new JObject
{
{"ContactID", crcontactID},
{"Photo1", ""}
}
: new JObject
{
{"ContactID", crcontactID},
{"Photo1", File.ReadAllBytes(crphoto1)}
};
答案 0 :(得分:2)
如果您只是在寻找如何检查文件是否存在,可以使用
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)}
};
}