當我手動選擇文件夾時,通過FolderBrowserDialog,我需要將路徑替換爲下面的代碼。如何使用所選文件夾的變量?
其中「%ProgramFiles(x86)%\ MyApp」必須替換爲所選文件夾+文件名的變量。
因此,選擇文件夾時,應顯示「label1」中的文件大小「testFile.txt」(將有兩個或更多此類文件)。
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
DialogResult result = folderBrowser.ShowDialog();
if (result == DialogResult.OK)
{
// Determine the size of the file in KB (dividing the number of bytes by 1024)
FileInfo fs1 = new FileInfo(Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%\\MyApp\\testFile.txt"));
long FileSize1 = fs1.Length/1024;
label1.Text = "testFile.txt (" + Convert.ToString(FileSize1) + " KB)";
if (FileSize1 > 180 & FileSize1 < 186) // If the file is larger and smaller than the specified sizes
{
label1.ForeColor = Color.Green;
}
else
{
label1.ForeColor = Color.Red;
}
}
}
決策,編輯:
FileInfo fs1 = new FileInfo(folderBrowser.SelectedPath + "\\testFile.txt");
或
FileInfo fs1 = new FileInfo(Path.Combine(folderBrowser.SelectedPath, "testFile.txt"));
對不起,對我來說這很複雜...... – Vitokhv
@Vitokhv你是什麼意思? – XenoRo
根據你的參考,我什麼都不懂,這個例子使用TextBox的代碼 – Vitokhv