我的项目有很多问题,我真的不知道从哪里开始。首先,我收到错误消息“非静态字段、方法或属性需要对象引用”。它在 retPath 下划线(行:DriveRecursion_results.DriveRecursion(retPath);)。我不知道如何解决这个问题。
我仍然难以理解的另一件事是如何在我的 Windows 窗体上填充 ListView 。我想要的是需要重命名的文件列表(相对于我列表中所有文件的列表。)
有人可以帮忙吗?几个小时以来,我一直在为此苦苦挣扎。
这是我的代码:
Form1.cs:
namespace FileMigration
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FolderSelect("Please select:");
}
public string FolderSelect(string txtPrompt)
{
//Value to be returned
string result = string.Empty;
//Now, we want to use the path information to population our folder selection initial location
string initialCheckoutPathDir = (@"C:\");
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(initialCheckoutPathDir);
FolderBrowserDialog FolderSelect = new FolderBrowserDialog();
FolderSelect.SelectedPath = info.FullName;
FolderSelect.Description = txtPrompt;
FolderSelect.ShowNewFolderButton = true;
if (FolderSelect.ShowDialog() == DialogResult.OK)
{
string retPath = FolderSelect.SelectedPath;
if (retPath == null)
{
retPath = "";
}
DriveRecursion_Results ds = new DriveRecursion_Results();
ds(retPath);
result = retPath;
//Close this form.
}
return result;
}
}
}
这是 DriveRecursion_Results.cs:
namespace FileMigration
{
public partial class DriveRecursion_Results : Form
{
public DriveRecursion_Results()
{
InitializeComponent();
}
private void fileOutput_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void DriveRecursion(string retPath)
{
// string[] files = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);
string pattern = " *[\\~#%&*{}/<>?|\"-]+ *";
string replacement = "";
Regex regEx = new Regex(pattern);
string[] fileDrive = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);
List<string> filePath = new List<string>();
foreach (string fileNames in fileDrive)
{
if (regEx.IsMatch(fileNames))
{
filePath.Add(fileNames);
//I tried adding my listview (fileOptions) here but I cannot for some reason
}
}
}
}
}
真的很感激任何帮助 :( 是否有人对如何更改我的代码以使其实际工作有任何想法?
请您参考如下方法:
问题 1:您的函数是静态的。如果它不再是这样,这将起作用。这是因为静态函数没有这个隐藏的“this”参数——它所作用的对象的引用。因此,它只能访问静态数据成员,而不能访问常规数据成员。
