Как получить переменную «SafeFileNames» из другого метода? В кнопке 1 У меня есть OpenFileDialog и в кнопке 2 я хочу получить все имена файлов.Как получить переменную «SafeFileNames» из другого метода?
Я использую цикл:
foreach (string fileName in openFileDialog1.SafeFileNames)
и в общественной форме частичной класса у меня есть:
public string[] fileName { get; private set; }
, но он не работает. Я мог бы создать новый массив и заполнить первый метод, но я ищу еще одно лучшее решение.
КОД:
У меня есть что-то вроде этого:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string[] fileName { get; private set; }
int numberOfFiles { get; set; }
public void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "txt";
openFileDialog1.Filter = "Text files (*.txt)|*.txt";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = false;
openFileDialog1.Multiselect = true;
textBox1.Text = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
int number = 0;
foreach (string fileName in openFileDialog1.SafeFileNames)
{
}
}
}
public void button2_Click(object sender, EventArgs e)
{
HERE - I want to know string from openFileDialog1.SafeFileNames
}
Теперь string[] fileName
является нулевым.
Вы хотите получить имена файлов в одной папке? Почему вы не используете OpenFolderDialog? –
'openFileDialog1.Multiselect = true;' определенные файлы не вся папка – test