关于C#:如何检查文件是否存在于Xamarin表单中

How to check if the file exist in xamarin forms

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

1
2
3
4
5
6
7
8
9
10
11
JObject ph1json = string.IsNullOrEmpty(crphoto1)
? new JObject
{
    {"ContactID", crcontactID},
    {"Photo1",""}
}
: new JObject
{
    {"ContactID", crcontactID},
    {"Photo1", File.ReadAllBytes(crphoto1)}
};


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    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)}
     };
    }