forked from mingkly/UdpShare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileManager.cs
81 lines (76 loc) · 2.19 KB
/
FileManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UdpQuickShare
{
public class FileManager
{
public static bool UseDirectPath { get;set; }
public static bool CanUseDirectPath => UseDirectPath && CanUseDirectPathPlatform();
public static Stream OpenReadFile(string platformPath,string directPath)
{
if(CanUseDirectPath)
{
try
{
return File.OpenRead(directPath);
}
catch
{
return MKFilePicker.MKFilePicker.OpenPickedFile(platformPath, "r");
}
}
else
{
return MKFilePicker.MKFilePicker.OpenPickedFile(platformPath, "r");
}
}
public static Stream OpenWriteFile(string platformPath, string directPath)
{
if (CanUseDirectPath)
{
return File.OpenWrite(directPath);
}
else
{
return MKFilePicker.MKFilePicker.OpenPickedFile(platformPath, "w");
}
}
public static Stream OpenReadWriteFile(string platformPath, string directPath)
{
if (CanUseDirectPath)
{
return File.Open(directPath,FileMode.OpenOrCreate);
}
else
{
return MKFilePicker.MKFilePicker.OpenPickedFile(platformPath, "rw");
}
}
static bool CanUseDirectPathPlatform()
{
#if WINDOWS
return true;
#elif ANDROID
return MauiFileSaver.Platforms.Android.Services.FileSaver.CanUseDirectFileApi();
#else
return false;
#endif
}
public static Task<bool> RequsetUseDirectPathNeed()
{
if (CanUseDirectPath)
{
return Task.FromResult(true);
}
#if ANDROID
return MauiFileSaver.Platforms.Android.Services.FileSaver.RequestUseDirerctFileApi();
#else
return Task.FromResult(true);
#endif
}
}
}