SevenZipSharp doesn't extract archive - c#

I'm trying to use SevenZipSharp from https://github.com/squid-box/SevenZipSharp to extract a zip archive. The dll setup is as follows:
public class Paths
{
private static readonly string SynthEBDexeDirPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
public static readonly string ResourcesFolderPath = Path.Combine(SynthEBDexeDirPath, "Resources");
// Toggle between the x86 and x64 bit dll
public readonly string SevenZipPath = Path.Combine(ResourcesFolderPath, "7Zip", Environment.Is64BitProcess ? "x64" : "x86", "7z.dll");
The dll files are copied into my Resources folder from the latest version of 7-Zip. The calling code looks as follows:
using System.IO;
using System.Windows.Controls;
using SevenZip;
namespace SynthEBD;
public class VM_ZipArchiveHandler : VM
{
public VM_ZipArchiveHandler(Window_SevenZipArchiveHandler window)
{
if (File.Exists(PatcherSettings.Paths.SevenZipPath))
{
SevenZipBase.SetLibraryPath(PatcherSettings.Paths.SevenZipPath);
Initialized = true;
}
else
{
CustomMessageBox.DisplayNotificationOK("Initialization Error", "Could not initialize Seven Zip from " + PatcherSettings.Paths.SevenZipPath);
}
Window = window;
}
public string DispString { get; set; } = string.Empty;
public ProgressBar Prog = new ProgressBar();
public Window_SevenZipArchiveHandler Window { get; set; }
private bool Initialized { get; set; } = false;
public void Unzip(string archivePath, string destinationFolder)
{
if (!Initialized)
{
return;
}
Prog.Minimum = 0;
Prog.Maximum = 100;
Prog.Value = 0;
Window.Show();
var progressHandler = new Progress<byte>(
percentDone => Prog.Value = percentDone);
var progress = progressHandler as IProgress<byte>;
var file = new SevenZipExtractor(archivePath);
file.Extracting += (sender, args) =>
{
progress.Report(args.PercentDone);
};
file.ExtractionFinished += (sender, args) =>
{
// Do stuff when done
};
Task.Run(() =>
{
//Extract the stuff
file.ExtractArchive(destinationFolder);
});
Window.Close();
}
public static void UnzipArchive(string archivePath, string destinationDir)
{
Window_SevenZipArchiveHandler window = new Window_SevenZipArchiveHandler();
VM_ZipArchiveHandler handler = new(window);
window.DataContext = handler;
handler.Unzip(archivePath, destinationDir);
}
}
I call UnzipArchive():
string tempFolderPath = Path.Combine(PatcherSettings.ModManagerIntegration.TempExtractionFolder, DateTime.Now.ToString("yyyy-MM-dd-HH-mm", System.Globalization.CultureInfo.InvariantCulture));
try
{
Directory.CreateDirectory(tempFolderPath);
}
catch (Exception ex)
{
Logger.LogError("Could not create or access the temp folder at " + tempFolderPath + ". Details: " + ex.Message);
return installedConfigs;
}
try
{
VM_ZipArchiveHandler.UnzipArchive(path, tempFolderPath);
}
In the end I get an empty directory; the .7z contents are never extracted to it. I've tried using both a .zip and .7z file as inputs, each containing two json files and nothing else. When I set a breakpoint at file.ExtractArchive(destinationFolder), it seems semi-correctly initialized: https://imgur.com/qjYpDur
It looks like it's correctly recognized as a SevenZip archive, but fields like _filesCount are null.
Am I doing something wrong with my setup?

I believe the issue is that your ExtractArchive is wrapped inside a Task and the calling thread returns before the extraction completes and isn't awaited. Not 100% on the details but as an experiment I found what works and what leaves the destination directory empty:
public static void Main(string[] args)
{
SevenZipBase.SetLibraryPath(
Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"7z.dll"));
string archivePath = "D:\\Downloads\\imgs.7z";
var file = new SevenZipExtractor(archivePath);
// works
file.ExtractArchive("D:\\Downloads\\unzipped");
// doesnt work
Task.Run(() =>
{
file.ExtractArchive("D:\\Downloads\\unzipped");
});
// works
Task.Run(() =>
{
file.ExtractArchive("D:\\Downloads\\unzipped");
}).Wait();
}

Related

How to handle multiple file downloads in Playwright?

I have a button that when clicked will start downloading multiple files (this button will also open a chrome://downloads tab and closes it immediately.
The page.download event handler for downloads will not fire.
The page.WaitForDownloadAsync() returns only one of these files.
I do not know the file names that will be downloaded, I also do not know if more than 1 file will be downloaded, there is always the possibility that only 1 file will be downloaded, but also the possibility that multiple files will be downloaded.
How can I handle this in playwright? I would like to return a list of all the downloaded files paths.
So I resolved this with the following logic.
I created two variables:
List<string> downloadedFiles = new List<string>();
List<string> fileDownloadSession = new();
I then created a method to add as a handler to the page.Download that looks like this:
private async void downloadHandler(object sender, IDownload download)
{
fileDownloadSession.Add("Downloading...");
var waiter = await download.PathAsync();
downloadedFiles.Add(waiter);
fileDownloadSession.Remove(fileDownloadSession.First());
}
Afterwards, I created a public method to get the downloaded files that looks like this:
public List<string> GetDownloadedFiles()
{
while (fileDownloadSession.Any())
{
}
var downloadedFilesList = downloadedFiles;
downloadedFiles = new List<string>();
return downloadedFilesList;
}
All these methods and planning are in a separate class of their own so that they can monitor the downloaded files properly, and also to freeze the main thread so it can grab all of the required files.
All in all it seems just as sketchy of a solution, similarly to how you would implement it in Selenium, nothing much has changed in terms of junkyard implementations in the new frameworks.
You can find my custom class here: https://paste.mod.gg/rztmzncvtagi/0, enjoy, there is no other topic that answers this specific question for playwright on C#.
Code here, in case it gets deleted from paste.mod.gg:
using System.Net;
using System.Runtime.InteropServices.JavaScript;
using Flanium;
using FlaUI.UIA3;
using Microsoft.Playwright;
using MoreLinq;
using Polly;
namespace Fight;
public class WebBrowser
{
private IBrowser _browser;
private IBrowserContext _context;
private IPage _page;
private bool _force;
private List<string> downloadedFiles = new List<string>();
private List<string> fileDownloadSession = new();
public void EagerMode()
{
_force = true;
}
public enum BrowserType
{
None,
Chrome,
Firefox,
}
public IPage GetPage()
{
return _page;
}
public WebBrowser(BrowserType browserType = BrowserType.Chrome, bool headlessMode = false)
{
var playwright = Playwright.CreateAsync().Result;
_browser = browserType switch
{
BrowserType.Chrome => playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions {Headless = headlessMode}).Result,
BrowserType.Firefox => playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions {Headless = headlessMode}).Result,
_ => null
};
_context = _browser.NewContextAsync().Result;
_page = _context.NewPageAsync().Result;
_page.Download += downloadHandler;
Console.WriteLine("WebBrowser was successfully started.");
}
private async void downloadHandler(object sender, IDownload download)
{
fileDownloadSession.Add("Downloading...");
var waiter = await download.PathAsync();
downloadedFiles.Add(waiter);
fileDownloadSession.Remove(fileDownloadSession.First());
}
public List<string> GetDownloadedFiles()
{
while (fileDownloadSession.Any())
{
}
var downloadedFilesList = downloadedFiles;
downloadedFiles = new List<string>();
return downloadedFilesList;
}
public void Navigate(string url)
{
_page.GotoAsync(url).Wait();
}
public void Close(string containedURL)
{
var pages = _context.Pages.Where(x => x.Url.Contains(containedURL));
if (pages.Any())
pages.ForEach(x => x.CloseAsync().Wait());
}
public IElementHandle Click(string selector, int retries = 15, int retryInterval = 1)
{
var element = Policy.HandleResult<IElementHandle>(result => result == null)
.WaitAndRetry(retries, interval => TimeSpan.FromSeconds(retryInterval))
.Execute(() =>
{
var element = FindElement(selector);
if (element != null)
{
try
{
element.ClickAsync(new ElementHandleClickOptions() {Force = _force}).Wait();
element.DisposeAsync();
return element;
}
catch (Exception e)
{
return null;
}
}
return null;
});
return element;
}
public IElementHandle FindElement(string selector)
{
IElementHandle element = null;
var Pages = _context.Pages.ToArray();
foreach (var w in Pages)
{
//============================================================
element = w.QuerySelectorAsync(selector).Result;
if (element != null)
{
return element;
}
//============================================================
var iframes = w.Frames.ToList();
var index = 0;
for (; index < iframes.Count; index++)
{
var frame = iframes[index];
element = frame.QuerySelectorAsync(selector).Result;
if (element is not null)
{
return element;
}
var children = frame.ChildFrames;
if (children.Count > 0 && iframes.Any(x => children.Any(y => y.Equals(x))) == false)
{
iframes.InsertRange(index + 1, children);
index--;
}
}
}
return element;
}
}

C#-Code in 2 Projects for a logger and a service, ERROR

I am trying to get my EasyLogger C# Project to work, but I can't debug this code because it is a Class Library. I use this logger in a Windows Console Application which is installed with squirrel as a Windows Service, so I can't see whats inside the variables....
What do I wrong?
This is in a Windows Console Application:
public void Start()
{
//Define EasyLogger
EasyLogger.Logger.Loggerpath = #"C:\Development\ServerSF";
EasyLogger.Logger.Logname = "Testlog";
Thread.Sleep(1000);
//Start EasyLogger
bool test = EasyLogger.Logger.Log;
EasyLogger.Logger.StartLog();
if (test == true)
{
//Start service
_timer.Start();
ExThred(configManager.ServerName);
}
else
{
//ERROR log isnt started
}
}
This is my EasyLogger Project:
using System;
using System.IO;
using System.Text;
namespace EasyLogger
{
public static class Logger
{
#region GET SET
private static string loggerpath;
private static string logname;
private static string message;
private static bool log;
public static string Loggerpath
{
get => loggerpath;
set => loggerpath = value;
}
public static string Logname
{
get => logname;
set => logname = value;
}
public static string Message
{
get => message;
set => message = value;
}
public static bool Log
{
get => log;
set => log = value;
}
#endregion
#region Logger
private static readonly string loggingpath = loggerpath + #"\logs\"+ logname + "-" + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + ".txt";
public static void StartLog()
{
if(loggerpath == null)
{
Message = "Program path is empty!";
log = false;
}
else
{
try
{
using (FileStream fs = File.Create(loggingpath))
{
byte[] info = new UTF8Encoding(true).GetBytes(DateTime.Now.ToString() + " - Logger v0.1 Started...");
fs.Write(info, 0, info.Length);
}
log = true;
}
catch
{
Message = "Can't create logfile!";
log = false;
}
}
}
public static void WriteLog(string msg)
{
if (Logname != null)
{
if (log == true)
{
string[] lines = new string[] { DateTime.Now.ToString() + " - " + msg };
File.AppendAllLines(loggingpath, lines);
}
else
{
try { Message = "Logger isn't started yet!"; }
catch { Message = "Logger Error!"; }
}
}
else { Message = "Please define first a Logname!"; }
}
public static void StopLog()
{
log = false;
}
#endregion
}
}
Your problem is not the logger. Your problem is how to debug a Windows Service.
With Visual Studio, you can do it this way:
Compile your Windows service with Debug informaton.
Install the Service and run it.
Start Visual Studio.
Select "Debug" --> "Attach Process"
Click "Show processes from all users"
Search for your service and click "Attach".
Select "Common Language Runtime"

How to wait for methods to finish then do new action?

I'm setting up my architechture to use Cef.Offscreen. In order to make it easy to work with I have divided some parts. But I run into a problem that controller loading finshes and serves a view before everything has been able to load.
Here's my structure --> Controller
public ActionResult InitBrowser()
{
ICefSharpRenderer renderer = RendererSingelton.GetInstance();
//Try to render something in default appdomain
renderer.LoginToTradingView(null, null);
ViewBag.SiteTitle = BrowserActions.RunScriptInNamedBrowser("loginbrowser", #"(function() {return document.title;} )();");
ViewBag.ImagesixtyfourUrl = BrowserActions.TakeScreenshot("loginbrowser");
//this is returned to fast, we have to wait for all
return View();
}
I have this class to get do some basic actions and initialize if needed.
public class CefSharpRenderer : MarshalByRefObject, ICefSharpRenderer
{
private ChromiumWebBrowser _browser;
private TaskCompletionSource<JavascriptResponse> _taskCompletionSource;
private string _name;
public void LoginToTradingView(string url, string browserName)
{
CheckIfCefIsInitialized();
BrowserFactory.GetBrowserInstance(#"https://se.tradingview.com/", "loginbrowser");
}
public void CreateBrowserAndGoToUrl(string url, string browserName)
{
CheckIfCefIsInitialized();
BrowserFactory.GetBrowserInstance(url, "browserName");
}
public void CheckIfCefIsInitialized()
{
if (!Cef.IsInitialized)
{
var settings = new CefSettings();
var assemblyPath = Path.GetDirectoryName(new Uri(GetType().Assembly.CodeBase).LocalPath);
settings.BrowserSubprocessPath = Path.Combine(assemblyPath, "CefSharp.BrowserSubprocess.exe");
settings.ResourcesDirPath = assemblyPath;
settings.LocalesDirPath = Path.Combine(assemblyPath, "locales");
var osVersion = Environment.OSVersion;
//Disable GPU for Windows 7
if (osVersion.Version.Major == 6 && osVersion.Version.Minor == 1)
{
// Disable GPU in WPF and Offscreen examples until #1634 has been resolved
settings.CefCommandLineArgs.Add("disable-gpu", "1");
}
//Perform dependency check to make sure all relevant resources are in our output directory.
Cef.Initialize(settings, performDependencyCheck: false, cefApp: null);
}
}
}
I get my browserinstance here and connected the events to be fired.
public static class BrowserFactory
{
public static ChromiumWebBrowser GetBrowserInstance(string _url, string browsername)
{
if (!BrowserContainer.CheckIfBrowserExists(browsername))
{
ChromiumWebBrowser _browser = new ChromiumWebBrowser(_url);
_browser.LoadingStateChanged += BrowserEvents.OnLoadingStateChanged;
BrowserContainer.AddDataHolder(browsername, new DataBrowserHolder { BrowserName = browsername, ChromiumWebBrow = _browser });
return _browser;
}
return null;
}
}
Browserevent loads correct page.
public static class BrowserEvents
{
public static void OnLoadingStateChanged(object sender, LoadingStateChangedEventArgs args)
{
if (args.IsLoading == false)
{
ChromiumWebBrowser cwb = (ChromiumWebBrowser)sender;
if (cwb.Address == "https://se.tradingview.com/")
{
BrowserActions.LogInToTradingView("xxxxx", "yyyyyyy", "loginbrowser");
}
}
}
}
Last my browseractions, spare med for the thread sleeps it's just under construction and it works atm.
public static class BrowserActions
{
public static void LogInToTradingView(string twusername, string twpassword, string browserName)
{
ChromiumWebBrowser _dataholder = BrowserContainer.GetDataHolderByName(browserName).ChromiumWebBrow;
IFrame ifww = _dataholder.GetMainFrame();
// var lull = #"(function() { var serielength = TradingView.bottomWidgetBar._widgets.backtesting._reportWidgetsSet.reportWidget._data.filledOrders.length; return serielength; })();";
// JavascriptResponse _js = Task.Run(async () => { return await _browser.GetMainFrame().EvaluateScriptAsync(lull); }).Result;
ifww.ExecuteJavaScriptAsync(#"(function() { window.document.getElementsByClassName('tv-header__link tv-header__link--signin js-header__signin')[0].click();})();");
// var loginusernamescript =
var loginpasswordscript = #"(function() { window.document.getElementsByClassName('tv-control-material-input tv-signin-dialog__input tv-control-material-input__control')[1].value= " + twpassword + "; })();";
var clkloginbtn = #"(function() { document.getElementsByClassName('tv-button tv-button--no-border-radius tv-button--size_large tv-button--primary_ghost tv-button--loader')[0].click();})();";
Thread.Sleep(300);
ifww.ExecuteJavaScriptAsync(#"(function() { window.document.getElementsByClassName('tv-control-material-input tv-signin-dialog__input tv-control-material-input__control')[0].click();})();");
Thread.Sleep(50);
ifww.ExecuteJavaScriptAsync(#"(function() { window.document.getElementsByClassName('tv-control-material-input tv-signin-dialog__input tv-control-material-input__control')[0].value = '" + twusername + "';})();");
Thread.Sleep(50);
ifww.ExecuteJavaScriptAsync(#"(function() { window.document.getElementsByClassName('tv-control-material-input tv-signin-dialog__input tv-control-material-input__control')[1].click();})();");
Thread.Sleep(50);
ifww.ExecuteJavaScriptAsync(#"(function() { window.document.getElementsByClassName('tv-control-material-input tv-signin-dialog__input tv-control-material-input__control')[1].value = '" + twpassword + "';})();");
Thread.Sleep(50);
ifww.ExecuteJavaScriptAsync(#"(function() { document.getElementsByClassName('tv-button tv-button--no-border-radius tv-button--size_large tv-button--primary_ghost tv-button--loader')[0].click();})();");
}
public static string TakeScreenshot(string browserName)
{
try
{
Bitmap img = Task.Run(async () => { return await BrowserContainer.GetDataHolderByName(browserName).ChromiumWebBrow.ScreenshotAsync(); }).Result;
// object mgss = img.Clone();
string baseen = ExtraFunctions.ToBase64String(img, ImageFormat.Png);
return baseen;
}
catch (Exception e)
{
var x = e.InnerException;
return null;
}
}
public static string RunScriptInNamedBrowser(string browserName, string script)
{
try
{
string str = Task.Run(async () => { return await BrowserContainer.GetDataHolderByName(browserName).ChromiumWebBrow.GetMainFrame().EvaluateScriptAsync(script); }).Result.ToString();
// object mgss = img.Clone();
return str;
}
catch (Exception e)
{
var x = e.InnerException;
return null;
}
}
}
How can I get my browser actions to report back to my controller so that I can wait for them to finish?
For a Task asynchronous operation to report back, it's possible to use Progress<T>. How that's done is detailed in Enabling Progress and Cancellation in Async APIs. The key is:
var progressIndicator = new Progress<int>(ReportProgress);
This creates a Progress<T> object that can indicate how far a task is complete, and also call a custom method (ReportProgress) at set intervals. You can create a custom class if necessary instead of using int.
So your browser actions can report back to the controller with the progress reporting method until everything is complete.

Extract an archive with progress bar?

How i can use an progress bar in this case?
void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
//System.Windows.MessageBox.Show("Update Complete!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
Uri uri = new Uri(url);
string filename = System.IO.Path.GetFileName(uri.AbsolutePath);
ZipFile.ExtractToDirectory(filePathDir + "/" + filename, filePathDir);
}
EDIT:
#Alessandro D'Andria , But in this case?:
WebClient wc = new WebClient();
Stream zipReadingStream = wc.OpenRead(url);
ZipArchive zip = new ZipArchive(zipReadingStream);
ZipFileExtensions.ExtractToDirectory(zip, filePathDir);
You can see the source of ExtractToDirectory on GitHub, the only thing you need to do is pass in a Progress<ZipProgress> and call it inside the foreach loop.
//This is a new class that represents a progress object.
public class ZipProgress
{
public ZipProgress(int total, int processed, string currentItem)
{
Total = total;
Processed = processed;
CurrentItem = currentItem;
}
public int Total { get; }
public int Processed { get; }
public string CurrentItem { get; }
}
public static class MyZipFileExtensions
{
public static void ExtractToDirectory(this ZipArchive source, string destinationDirectoryName, IProgress<ZipProgress> progress)
{
ExtractToDirectory(source, destinationDirectoryName, progress, overwrite: false);
}
public static void ExtractToDirectory(this ZipArchive source, string destinationDirectoryName, IProgress<ZipProgress> progress, bool overwrite)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (destinationDirectoryName == null)
throw new ArgumentNullException(nameof(destinationDirectoryName));
// Rely on Directory.CreateDirectory for validation of destinationDirectoryName.
// Note that this will give us a good DirectoryInfo even if destinationDirectoryName exists:
DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
string destinationDirectoryFullPath = di.FullName;
int count = 0;
foreach (ZipArchiveEntry entry in source.Entries)
{
count++;
string fileDestinationPath = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, entry.FullName));
if (!fileDestinationPath.StartsWith(destinationDirectoryFullPath, StringComparison.OrdinalIgnoreCase))
throw new IOException("File is extracting to outside of the folder specified.");
var zipProgress = new ZipProgress(source.Entries.Count, count, entry.FullName);
progress.Report(zipProgress);
if (Path.GetFileName(fileDestinationPath).Length == 0)
{
// If it is a directory:
if (entry.Length != 0)
throw new IOException("Directory entry with data.");
Directory.CreateDirectory(fileDestinationPath);
}
else
{
// If it is a file:
// Create containing directory:
Directory.CreateDirectory(Path.GetDirectoryName(fileDestinationPath));
entry.ExtractToFile(fileDestinationPath, overwrite: overwrite);
}
}
}
}
This is used like
public class YourClass
{
public Progress<ZipProgress> _progress;
public YourClass()
{
// Create the progress object in the constructor, it will call it's ReportProgress using the sync context it was constructed on.
// If your program is a UI program that means you want to new it up on the UI thread.
_progress = new Progress<ZipProgress>();
_progress.ProgressChanged += Report
}
private void Report(object sender, ZipProgress zipProgress)
{
//Use zipProgress here to update the UI on the progress.
}
//I assume you have a `Task.Run(() => Download(url, filePathDir);` calling this so it is on a background thread.
public void Download(string url, string filePathDir)
{
WebClient wc = new WebClient();
Stream zipReadingStream = wc.OpenRead(url);
ZipArchive zip = new ZipArchive(zipReadingStream);
zip.ExtractToDirectory(filePathDir, _progress);
}
//...
Maybe something like this can work for you:
using (var archive = new ZipArchive(zipReadingStream))
{
var totalProgress = archive.Entries.Count;
foreach (var entry in archive.Entries)
{
entry.ExtractToFile(destinationFileName); // specify the output path of thi entry
// update progess there
}
}
It's simple a workaround to keep track of the progress.

Explanation on Adding items to a ListBox in Windows RT

I am trying to make a simple Windows 8/RT app for the store and i have a question about adding items to a ListBox.
In my MainPage i have this code:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.brain = new MainController();
LoadData();
}
public void LoadData()
{
brain.GetNotesRepoFile().ReadFile();
Debug(""+brain.GetNotesRepoFile().GetNotesList().Count);
for(int i = 0; i < brain.GetNotesRepoFile().GetNotesList().Count; i++)
{
notesListBox.Items.Add( // code here );
}
}
}
public class NotesRepositoryFile
{
// CONSTRUCTOR
public NotesRepositoryFile()
{
this.notesRepository = new List<Note>();
}
// Read from file
public async void ReadFile()
{
// settings for the file
var path = #"Files\Notes.txt";
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// acquire file
var file = await folder.GetFileAsync(path);
var readThis = await Windows.Storage.FileIO.ReadLinesAsync(file);
foreach (var line in readThis)
{
notesRepository.Add(new Note(line.Split(';')[0], line.Split(';')[1]));
// check if the item was added
Debug.WriteLine("Added: " + notesRepository[notesRepository.Count - 1].ToString());
}
Debug.WriteLine("File read successfully");
}
}
My Output is:
0
Added: Test1
Added: Test2
File read successfully
What am i trying to do here is read strings from a file and add them using Items.Add to a listBox. But since the size of the array is 0, even though the items were added successfully that doesnt work.
I dont understand why Debug(""+brain.GetNotesRepoFile().GetNotesList().Count); is executed before brain.GetNotesRepoFile().ReadFile(); since clearly that is not the case.
Also why does this solution work, and the above doesnt ??
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.brain = new MainController();
ReadFile();
}
// Read from file
public async void ReadFile()
{
// settings for the file
var path = #"Files\Notes.txt";
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// acquire file
var file = await folder.GetFileAsync(path);
var readThis = await Windows.Storage.FileIO.ReadLinesAsync(file);
foreach (var line in readThis)
{
brain.AddNote(line.Split(';')[0], line.Split(';')[1]);
notesListBox.Items.Add(brain.GetNotesRepoFile().GetNotesList()[brain.GetNotesRepoFile().GetNotesList().Count - 1].ToString());
}
Debug.WriteLine("File read successfully");
}
}
Well, usage of async and await is wrong is you code, please change according to following codes
First, in NotesRepositoryFile class
public async Task<bool> ReadFile()
{
//Your code
if (notesRepository.Count > 0) return true;
return false;
}
Second in the MainPage
public async void LoadData()
{
bool HasNote = await brain.GetNotesRepoFile().ReadFile();
if (HasNote)
{
for (int i = 0; i < brain.GetNotesRepoFile().notesRepository.Count; i++)
{
//Your code
}
}
}

Categories