【问题标题】:How to check if the file exist in xamarin forms如何检查文件是否以 xamarin 形式存在
【发布时间】:2019-02-13 05:24:57
【问题描述】:

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

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

【问题讨论】:

  • 所以你只想检查设备中是否存在文件?

标签: c# xamarin xamarin.forms


【解决方案1】:

如果您只是在寻找如何检查文件是否存在,您可以使用

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

【讨论】:

  • 在循环中如果文件不存在我想跳过,我只添加return怎么样?
  • 使用return可以跳过循环
  • 你也可以使用继续;或打破;取决于你真正在做什么
  • crphoto1 包含图像的路径文件如果我使用这个“bool doesExist = File.Exists(crphoto1);”可以吗?
猜你喜欢
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
相关资源
最近更新 更多