How to check if the file exist in xamarin forms

你说的曾经没有我的故事 提交于 2021-01-28 06:50:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!