C# ListView的问题&;文件输出
我的项目有很多问题,我真的不知道从哪里开始。首先,我得到一个错误“非静态字段、方法或属性需要对象引用”。它在retPath(行:DriveRecursion_results.DriveRecursion(retPath);)下面加下划线。我不知道如何解决这个问题 另一件我仍然困惑的事情是如何在我的Windows窗体上填充listview。我想要的是需要重命名的文件列表(而不是列表中所有文件的列表) 有人能帮忙吗?我已经痛苦地挣扎了好几个小时了 这是我的密码: 表格1.cs:C# ListView的问题&;文件输出,c#,winforms,listview,C#,Winforms,Listview,我的项目有很多问题,我真的不知道从哪里开始。首先,我得到一个错误“非静态字段、方法或属性需要对象引用”。它在retPath(行:DriveRecursion_results.DriveRecursion(retPath);)下面加下划线。我不知道如何解决这个问题 另一件我仍然困惑的事情是如何在我的Windows窗体上填充listview。我想要的是需要重命名的文件列表(而不是列表中所有文件的列表) 有人能帮忙吗?我已经痛苦地挣扎了好几个小时了 这是我的密码: 表格1.cs: namespace
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;
}
}
}
private void 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.DriveRecursion( retPath );
ds.Show();
result = retPath;
//Close this form.
}
return;
}
以下是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
}
}
}
}
}
命名空间文件迁移
{
公共部分类驱动递归_结果:表单
{
公共驱动递归_结果()
{
初始化组件();
}
私有无效文件输出\u SelectedIndexChanged(对象发送方,事件参数e)
{
}
公共void驱动递归(字符串retPath)
{
//字符串[]files=Directory.GetFiles(retPath,“***”,SearchOption.AllDirectories);
字符串模式=“*[\\~\\\%&*{}/?\\”-]+*”;
字符串替换=”;
正则表达式正则表达式=新正则表达式(模式);
字符串[]fileDrive=Directory.GetFiles(retPath,“**”,SearchOption.AllDirectories);
列表文件路径=新列表();
foreach(fileDrive中的字符串文件名)
{
if(regEx.IsMatch(文件名))
{
添加(文件名);
//我尝试在这里添加我的listview(fileOptions),但由于某些原因我无法添加
}
}
}
}
}
任何帮助都将不胜感激:(有人对如何更改我的代码以使其实际工作有任何想法吗?问题1:您的函数是静态的。如果它不再是静态的,这将起作用。这是因为静态函数没有这个隐藏的“this”参数—它所作用的对象的引用。因此,它只能访问静态数据成员,而不能访问常规成员。问题1:您的函数是静态的。如果它不再是静态的,这将起作用。这是因为静态函数没有这个隐藏的“this”参数—它所作用的对象的引用。因此,它只能访问静态数据成员,而不能访问常规数据成员。您不能从该级别将项目添加到listview,因为listview是非静态的,并且方法DriveRecursion是静态的。我将首先将DriveRecursion方法更改为非静态或返回文件路径列表。您不能从该级别将项目添加到listview,因为listview是非静态的,而方法DriveRecursion是静态的。我将首先将DriveRecursion方法更改为非静态或重新设置打开文件路径列表。您无法将项目添加到列表视图,因为您试图从静态方法添加项目 因为它是静态的,所以没有
列表视图
,因为实际上没有一个表单
来添加内容。为了将内容添加到列表视图
,您需要使驱动递归()
不是静态的
此外,当您使DriveRecursion()
不是静态的时,您需要一种方法让Form1
知道要填充哪个DriveRecursion\u结果
类
另一种方法是让Form1
返回retPath
到DriveRecursion\u结果
编辑
删除了我原来答案中不相关的部分
我复制了您的代码,完全按照您发布的方式。然后在运行此代码时对Form1.cs中的FolderSelect()进行了以下更改。我可以弹出第二个窗口,但不能关闭另一个窗口,因为这将导致应用程序退出
请确保您有ds.Show()并在某个时候调用ds.DriveRecursion(retPath)
在Form1.cs中修改了FolderSelect(字符串):
private void 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.DriveRecursion( retPath );
ds.Show();
result = retPath;
//Close this form.
}
return;
}
您无法将项目添加到列表视图中,因为您正试图从静态方法添加项目 因为它是静态的,所以没有
列表视图
,因为实际上没有一个表单
来添加内容。为了将内容添加到列表视图
,您需要使驱动递归()
不是静态的
此外,当您使DriveRecursion()
不是静态的时,您需要一种方法让Form1
知道要填充哪个DriveRecursion\u结果
类
另一种方法是让Form1
返回retPath
到DriveRecursion\u结果
编辑
删除了我原来答案中不相关的部分
我复制了您的代码,完全按照您发布的方式。然后在运行此代码时对Form1.cs中的FolderSelect()进行了以下更改。我可以弹出第二个窗口,但不能关闭另一个窗口,因为这将导致应用程序退出
请确保您有ds.Show()并在某个时候调用ds.DriveRecursion(retPath)
在Form1.cs中修改了FolderSelect(字符串):
private void 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.DriveRecursion( retPath );
ds.Show();
result = retPath;
//Close this form.
}
return;
}
我摆脱了静态——因此这两个方法现在都是非静态的。但是,我仍然在同一个位置得到“非静态字段、方法或属性需要对象引用”)我猜您将剩下的代码放在一边,因此仍然有DriveRecursion\u Result.DriveRecursion(retPath)。您现在需要一个特定的DriveRecursion\u结果来调用DriveRecursion。您需要有一个DriveRecursion\u结果类的实例并在该类上调用DriveRecursion。我去掉了static,所以这两个方法现在都是非静态的。但是,我仍然得到“非静态字段、方法或属性需要对象引用”)在同一个确切的位置,我的猜测是您将代码的其余部分单独留下,因此您仍然有DriveRecursion\u Result.DriveRecursion(retPath)。您现在需要一个特定的DriveRecursion\u Result来调用DriveRecursion o