如何将列表框中的选定项用作新生成的文本文件的名称

本文关键字:新生 文本 文件 列表 | 更新日期: 2023-09-27 18:36:55

如何使用列表框中的选定项作为新生成的文本文件的名称?(可视 C#)

这是一段代码:

string path = @"C:''Public Key Pin'Test.txt";
if (!File.Exists(path))
{
    File.Create(path);
    using (StreamWriter sw = File.CreateText(path))
    {
        sw.WriteLine("The first line!");
    }
}
else if (File.Exists(path))
{
    MessageBox.Show("File with this path already exists.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

如何将列表框中的选定项用作新生成的文本文件的名称

假设您的列表框 ID 是ListBox1

在 asp.net然后尝试

string path = "C:''Public Key Pin'"+ListBox1.SelectedItem.Text+".txt

(温福斯)试试这个

string path = "C:''Public Key Pin'"+ListBox1.GetItemText(ListBox1.SelectedItem)+".txt

试试这个:

ListBox ls = new ListBox();
string path = @"C:''Public Key Pin'" + ls.SelectedItem.ToString() + ".txt";

并覆盖所选类中的 ToString() 方法。