Оптимизация повторяющегося кода в методе
Подскажите, пожалуйста, как правильно оформить повторяющийся код вызова диалогового окна в методе ButFurther_Click(вызывается после нажатия кнопки в WindowsForms)? Я пытался оформить в метод внутри метода ButFurther_Click, но это не работает.
using System;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
namespace Anomalies
{
public partial class Form1 : Form
{
//private string openPath;
public Form1()
{
InitializeComponent();
}
private void ButFurther_Click(object sender, EventArgs e)
{
string pathFarther = @"C:\Python";
if (Directory.Exists(pathFarther))
{
if (File.Exists(pathFarther + @"\python.exe"))
{
string path = pathFarther + @"\python.exe";
// запуск распознавания аномалий
string fileName = "python\\open_window.py";
if (File.Exists(fileName))
{
Process p = new Process();
p.StartInfo = new ProcessStartInfo(path, fileName)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
else
{
MessageBox.Show("Не найден файл open_window.py");
}
}
else
{
MessageBox.Show("Выберите другой каталог, куда установлен Python или установите его");
SetPath();
}
}
else
{
MessageBox.Show("Выберите каталог, куда установлен Python или установите его");
SetPath();
}
string SetPath()
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
DialogResult result = folderBrowserDialog.ShowDialog();
if (result == DialogResult.OK)
{
string path = folderBrowserDialog.SelectedPath;
MessageBox.Show(path);
}
}
}
}
}
Оформил в метод SetPath(), но VS 2019 пишет "не все пути к коду возвращают значение".