Skip to content

Commit 1b8a23d

Browse files
author
Rustam Zaitov
committed
[WorkingWithFiles] fix #33057
1 parent a304680 commit 1b8a23d

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

WorkingWithFiles/PCL/Android/SaveAndLoad_Android.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,29 @@ public class SaveAndLoad_Android : ISaveAndLoad
1515

1616
public async Task SaveTextAsync (string filename, string text)
1717
{
18-
var docsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
19-
var path = Path.Combine(docsPath, filename);
20-
21-
using (StreamWriter sw = File.CreateText(path))
22-
{
18+
var path = CreatePathToFile (filename);
19+
using (StreamWriter sw = File.CreateText (path))
2320
await sw.WriteAsync(text);
24-
}
2521
}
2622

2723
public async Task<string> LoadTextAsync (string filename)
2824
{
29-
string text;
30-
var docsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
31-
var path = Path.Combine(docsPath, filename);
32-
25+
var path = CreatePathToFile (filename);
3326
using (StreamReader sr = File.OpenText(path))
34-
{
35-
text = await sr.ReadToEndAsync();
36-
}
37-
return text;
27+
return await sr.ReadToEndAsync();
28+
}
29+
30+
public bool FileExists (string filename)
31+
{
32+
return File.Exists (CreatePathToFile (filename));
3833
}
3934

4035
#endregion
41-
}
42-
}
4336

37+
string CreatePathToFile (string filename)
38+
{
39+
var docsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
40+
return Path.Combine(docsPath, filename);
41+
}
42+
}
43+
}

WorkingWithFiles/PCL/WorkingWithFiles/ISaveAndLoad.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public interface ISaveAndLoad
1414
{
1515
Task SaveTextAsync (string filename, string text);
1616
Task<string> LoadTextAsync (string filename);
17+
bool FileExists (string filename);
1718
}
1819
}
1920

WorkingWithFiles/PCL/WorkingWithFiles/SaveAndLoadText.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ namespace WorkingWithFiles
1111
/// </summary>
1212
public class SaveAndLoadText : ContentPage
1313
{
14+
const string fileName = "temp.txt";
1415
Button loadButton, saveButton;
1516

1617
public SaveAndLoadText ()
1718
{
19+
var fileService = DependencyService.Get<ISaveAndLoad> ();
20+
1821
var input = new Entry { Text = "" };
1922
var output = new Label { Text = "" };
2023
saveButton = new Button {Text = "Save"};
@@ -23,8 +26,8 @@ public SaveAndLoadText ()
2326
loadButton.IsEnabled = saveButton.IsEnabled = false;
2427
// uses the Interface defined in this project, and the implementations that must
2528
// be written in the iOS, Android and WinPhone app projects to do the actual file manipulation
26-
await DependencyService.Get<ISaveAndLoad>().SaveTextAsync("temp.txt", input.Text);
2729

30+
await fileService.SaveTextAsync (fileName, input.Text);
2831
loadButton.IsEnabled = saveButton.IsEnabled = true;
2932
};
3033

@@ -34,9 +37,10 @@ public SaveAndLoadText ()
3437

3538
// uses the Interface defined in this project, and the implementations that must
3639
// be written in the iOS, Android and WinPhone app projects to do the actual file manipulation
37-
output.Text = await DependencyService.Get<ISaveAndLoad>().LoadTextAsync("temp.txt");
40+
output.Text = await fileService.LoadTextAsync(fileName);
3841
loadButton.IsEnabled = saveButton.IsEnabled = true;
3942
};
43+
loadButton.IsEnabled = fileService.FileExists (fileName);
4044

4145
var buttonLayout = new StackLayout {
4246
Orientation = StackOrientation.Horizontal,

WorkingWithFiles/PCL/iOS/SaveAndLoad_iOS.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,28 @@ public static string DocumentsPath {
2424

2525
public async Task SaveTextAsync (string filename, string text)
2626
{
27-
string path = BuildPathForDocumentsDir (filename);
27+
string path = CreatePathToFile (filename);
2828
using (StreamWriter sw = File.CreateText(path))
29-
{
3029
await sw.WriteAsync(text);
31-
}
3230
}
3331

3432
public async Task<string> LoadTextAsync (string filename)
3533
{
36-
string path = BuildPathForDocumentsDir (filename);
34+
string path = CreatePathToFile (filename);
3735
using (StreamReader sr = File.OpenText(path))
38-
{
3936
return await sr.ReadToEndAsync();
40-
}
37+
}
38+
39+
public bool FileExists (string filename)
40+
{
41+
return File.Exists (CreatePathToFile (filename));
4142
}
4243

4344
#endregion
4445

45-
static string BuildPathForDocumentsDir(string fileName)
46+
static string CreatePathToFile(string fileName)
4647
{
4748
return Path.Combine (DocumentsPath, fileName);
4849
}
4950
}
50-
}
51-
51+
}

0 commit comments

Comments
 (0)