приложение запускает медиаплеер в случае файлов mp3. >
но процесс по какой-то причине равен нулю
вот моя MainViewModel
Код: Выделить всё
[ObservableProperty]
string? folderPath;
[ObservableProperty]
FileItem? selectedItem;
[ObservableProperty]
double progress;
[ObservableProperty]
bool isEnable = true;
public ObservableCollection Files { get; set; } = [];
public List
? Processes { get; set; } = [];
[RelayCommand]
async Task CleanFolder()
{
await DeleteNonMp4files(FolderPath!);
}
[RelayCommand]
void BrowseFolders()
{
var dlg = new OpenFolderDialog();
if (dlg.ShowDialog() == true)
{
FolderPath = dlg.FolderName;
GetFiles(FolderPath);
}
}
public void GetFiles(string folderPath)
{
{
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var projectRoot = Directory.GetParent(baseDirectory)?.Parent?.Parent?.Parent?.FullName;
string extensionsIconsFolderPath = Path.Combine(projectRoot!, "Ext");
if (!Directory.Exists(extensionsIconsFolderPath))
{
throw new DirectoryNotFoundException($"The Extensions folder was not found at {projectRoot}");
}
Files.Clear();
var filesInDirr = Directory.GetFiles(folderPath);
foreach (var file in filesInDirr)
{
string extension = Path.GetExtension(file).ToLower();
string iconPath = GetIconPathForExtension(extension, extensionsIconsFolderPath);
Files.Add(new FileItem
{
Name = Path.GetFileNameWithoutExtension(file),
Extention = extension,
FullPath = Path.GetFullPath(file),
Icon = iconPath,
});
}
}
}
public async Task DeleteNonMp4files(string folderPath)
{
IsEnable = false;
if (Directory.Exists(folderPath))
{
var filesInFolder = Directory.GetFiles(folderPath);
var totalFiles = Files.Count;
var filesDeleted = 0;
foreach (var item in filesInFolder)
{
if (!Path.GetExtension(item).Equals(".mp4", StringComparison.CurrentCultureIgnoreCase))
{
File.Delete(item);
filesDeleted++;
Progress = (double)filesDeleted / totalFiles * 100;
await Task.Delay(50);
}
}
GetFiles(folderPath);
Progress = 100;
if (Progress == 100)
{
MessageBox.Show("Done");
IsEnable = true;
}
}
}
private string GetIconPathForExtension(string extension, string extensionsFolder)
{
string iconFileName = extension switch
{
".avi" => "avi.png",
".mp4" => "mp4.png",
".mp3" => "mp3.png",
".pdf" => "pdf.png",
".png" => "png.png",
".txt" => "txt.png",
".wav" => "wav.png",
_ => "question.png",// Fallback icon if no match is found
};
// Combine the Extensions folder path with the icon file name
string iconPath = Path.Combine(extensionsFolder, iconFileName);
// Check if the icon exists, otherwise return a default icon
if (!File.Exists(iconPath))
{
iconPath = Path.Combine(extensionsFolder, "default.png"); // Fallback to default icon if not found
}
return iconPath;
}
partial void OnSelectedItemChanged(FileItem? value)
{
if (!string.IsNullOrEmpty(value!.FullPath))
{
var p = Process.Start(new ProcessStartInfo
{
FileName = value.FullPath,
UseShellExecute = true
});
Task.Delay(5000);
Processes!.Add(p!);
}
}
public void CleanUpProcesses()
{
foreach (var process in Processes!)
{
try
{
if (!process.HasExited)
{
process.Kill(); // Kill the process
}
}
catch (Exception ex)
{
// Handle errors during cleanup (e.g., process already exited)
Console.WriteLine($"Error terminating process: {ex.Message}");
}
и я уже пробовал, почему попытка блокировки денег и ничего
Подробнее здесь: https://stackoverflow.com/questions/792 ... to-open-th