2010-06-22 33 views
1

我有很多与我的项目有关的问题,我真的不知道从哪里开始。首先,我得到一个错误“非静态字段,方法或属性需要对象引用”。它强调了retPath(该行:DriveRecursion_results.DriveRecursion(retPath);)。我不知道如何解决这个问题。ListView和文件输出的问题

我仍然难住的另一件事是如何填充我的Windows窗体上的列表视图。我想要的是需要重命名的文件列表(与我列表中的所有文件列表相比较)。

任何人都可以帮忙吗?现在我一直在苦苦挣扎几个小时。

这里是我的代码:

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 
      } 
     } 

     } 





     } 




    } 

任何帮助将真正理解:(没有任何人有任何想法关于如何更改我的代码使其实际上有效?

+0

编辑:现在这两种方法都是非静态的,并照顾了我用retPath得到的错误。然而,任何人都有问题#2的想法,让列表视图仅显示需要重命名的文件的输出? – yeahumok 2010-06-22 20:48:08

+0

你有什么代码已经尝试添加项目到你的列表视图?现在该方法不是静态的,你应该只能调用ListView.Items.Add(“Item”); – 2010-06-22 20:53:40

+0

我这样做 - 但是当我运行我的应用程序,我只有对话选择驱动器和文件夹,而不是列表。是它的B/C我没有把私人无效的任何东西fileOutput_SelectedIndexChanged(对象发件人,EventArgs E)?如果是这种情况,我需要在那里确保我的应用程序知道要显示带有需要更改的文件输出的列表视图。 – yeahumok 2010-06-22 20:56:40

回答

0

您无法将项目添加到您的列表视图,因为您正尝试从静态方法添加它们。

由于它是静态的,所以没有ListView,因为实际上没有Form来添加东西。您需要将DriveRecursion()设为非静态才能将其添加到ListView

此外,当您将DriveRecursion()设为非静态时,您需要一种方法让Form1知道要填充哪个DriveRecursion_Results类。

您可以采取的另一种方法是让Form1返回retPathDriveRecursion_Results

编辑

删除我原来的答复的不相关部分

我抄你的代码究竟如何张贴。然后对Form1.cs中的FolderSelect()进行以下更改当我运行此代码时。我可以弹出第二个窗口,但不能关闭另一个窗口,因为这会导致应用程序退出。

请确保您有ds.Show(),并在Form1的一些点呼叫ds.DriveRecursion(retPath)

修改FolderSelect(串)做的。CS:

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; 
    } 
+0

我仍然无法让我的初始Form1关闭。一旦用户在文件浏览器对话框上按下“确定”,表单将关闭并移至下一个表单(需要重命名的文件列表)。 – yeahumok 2010-06-23 13:37:50

+0

我猜测您在尝试关闭表单时遇到同样的问题与此.Close() - 整个应用程序退出,这是因为启动应用程序Form1的表单现在已关闭,因此应用程序退出之前您有机会s ee DriveRecursion_Results。我会在几秒钟后发布一个修改。 – 2010-06-23 13:47:00

+0

其实我的应用程序不关闭 - 没有关闭。 Form1不关闭(包含用户按下以提示FileBrowserDialog的单个按钮的表单)。即使用户选择了他们希望应用程序钻取的驱动器和文件夹后,似乎也没有任何反应。 – yeahumok 2010-06-23 13:48:19

2

问题1:您的功能是stati C。如果它停止这样,这将工作。这是因为一个静态函数没有这个隐藏的'this'参数 - 对它所作用的对象的引用。因此,它只能访问静态数据成员,而不是常规数据成员。

+0

我摆脱了静态 - 所以这两种方法现在都是非静态的。但是,我仍然在同一个确切位置获得“非静态字段,方法或属性需要的对象引用) – yeahumok 2010-06-22 20:42:14

+1

我的猜测是,您只剩下其余代码,因此您仍然有DriveRecursion_Result.DriveRecursion( retPath)现在你需要一个特定的DriveRecursion_Result来调用DriveRecursion,你需要有一个DriveRecursion_Result类的实例并且调用DriveRecursion。 – 2010-06-22 20:46:43

2

您无法将这些项目添加到您的列表视图中,因为listview是非静态的,并且DriveRecursion方法是静态的。我将首先将DriveRecursion方法更改为非静态或返回文件路径列表。