如何检查文件是否以Xamarin形式存在

时间:2019-02-13 05:24:57

标签: c# xamarin xamarin.forms

我想检查设备内部的文件是否存在。当变量 crphoto1 为空或文件不存在时,“ Photo1 ” json应为{"Photo1", ""}

JObject ph1json = string.IsNullOrEmpty(crphoto1)
? new JObject
{
    {"ContactID", crcontactID},
    {"Photo1", ""}
}
: new JObject
{
    {"ContactID", crcontactID},
    {"Photo1", File.ReadAllBytes(crphoto1)}
};

1 个答案:

答案 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)}
     };
    }