does anyone knows how I can simplify this code?
The objective is to get multiple files from the explorer and validate the number of files, the weight of each file and the total weight.
public static async Task<IEnumerable<StorageFile>> GetMultipleFileFromDisk(Window rootWindow, int maxFiles,
string fileTypes = null, ulong fileMaxSize = 0, ulong totalMaxSize = 0,
PickerLocationId? pickerLocation = null, PickerViewMode viewMode = PickerViewMode.List)
{
var filePicker = new FileOpenPicker();
if (fileTypes != null)
{
var fileTypesArray = fileTypes.Split(";");
//IList<string> fileTypesList = fileTypes.Split(";");
// Get the current window's Handler by passing in the Window object
var handler = WinRT.Interop.WindowNative.GetWindowHandle(rootWindow);
// Associate the HWND with the file picker
WinRT.Interop.InitializeWithWindow.Initialize(filePicker, handler);
if (fileTypesArray.Count() >= 0)
foreach (var fileType in fileTypesArray)
filePicker.FileTypeFilter.Add(fileType);
}
filePicker.SuggestedStartLocation = pickerLocation ?? PickerLocationId.DocumentsLibrary;
filePicker.ViewMode = viewMode;
var files = await filePicker.PickMultipleFilesAsync();
if (files.Count > maxFiles)
throw new Exception($"Só pode carregar {maxFiles} ficheiros.");
IEnumerable<StorageFile> finalFiles;
if (fileMaxSize != 0)
{
var correctFiles = new List<StorageFile>();
foreach (var file in files)
{
var hasRightSize = await HasRightSize(file, fileMaxSize);
if (hasRightSize)
correctFiles.Add(file);
else
throw new Exception(file.Name + " excede o tamanho máximo.");
}
finalFiles = correctFiles;
} else
finalFiles = files;
if (totalMaxSize <= 0) return finalFiles;
if (!await HasRightSize(finalFiles, totalMaxSize))
finalFiles = null;
return finalFiles;
}