如何创建详细说明文件及其子目录的程序?

时间:2011-06-21 18:02:48

标签: visual-c#-express-2010

有一个我正在研究的程序,即使在如何开始这个方面我也绝对失败了。我使用的是Visual Studio C#Windows App Form。

我需要做的是允许用户输入他们想要的任何路径位置,程序将返回文件/文件夹的名称;路径;日期和大小,这也将在子目录中完成。

我在MSDN网站上找到了一些代码,我正在尝试使用它并在本项目的第一部分修改它,但不断收到错误消息。一些消息表明存在多个条目,即(static void Main()和using namespace Detailed)。

这是我到目前为止所拥有的一个带有富文本框和FolderBrowserDialog的表单,看起来我没有这么多错误就无法超越这一点。

这是在Form1.Designer.cs:

<i>namespace Detailed

{     部分类Form1     {         ///         ///所需的设计变量。         ///         private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();
        this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
        this.SuspendLayout();
        // 
        // folderBrowserDialog1
        // 
        this.folderBrowserDialog1.HelpRequest += new System.EventHandler(this.folderBrowserDialog1_HelpRequest);
        // 
        // richTextBox1
        // 
        this.richTextBox1.Location = new System.Drawing.Point(12, 32);
        this.richTextBox1.Name = "richTextBox1";
        this.richTextBox1.Size = new System.Drawing.Size(167, 23);
        this.richTextBox1.TabIndex = 0;
        this.richTextBox1.Text = "";
        // 
        // openFileDialog1
        // 
        this.openFileDialog1.FileName = "openFileDialog1";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.richTextBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
    private System.Windows.Forms.RichTextBox richTextBox1;
    private System.Windows.Forms.OpenFileDialog openFileDialog1;
}

}

对于For1.cs,这是我到目前为止所做的:

使用系统; 使用System.Collections.Generic; 使用System.ComponentModel; 使用System.Data; 使用System.Drawing; 使用System.Linq; 使用System.Text; 使用System.Windows.Forms; 使用System.IO;

public class FolderBrowserDialogExampleForm:System.Windows.Forms.Form {     private FolderBrowserDialog folderBrowserDialog1;     private OpenFileDialog openFileDialog1;

private RichTextBox richTextBox1;

private MainMenu mainMenu1;
private MenuItem fileMenuItem, openMenuItem;
private MenuItem folderMenuItem, closeMenuItem;

private string openFileName, folderName;

private bool fileOpened = false;

public partial class Form1 : Form
{
    public Form1()
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
    {

    }
}

private void InitializeComponent()
{
    this.SuspendLayout();
    // 
    // FolderBrowserDialogExampleForm
    // 
    this.ClientSize = new System.Drawing.Size(284, 262);
    this.Name = "FolderBrowserDialogExampleForm";
    this.ResumeLayout(false);

}

}

我仍然是编程的新手,希望我可以尽快得到这个,因为我被要求在星期四早上使用它。我在表单中有Rich TextBox,但由于错误太多而删除了它。

这是我找到的代码。我知道这只是我需要做的事情的一部分,但在阅读代码时,我注意到也许我可以应用表单所需的内容然后分解代码并将代码片段放在我需要的地方。 This is the code I am following

以下是我使用Form1.Designer.cs收到的错误消息 - 其中有14个错误: 'detailed.form1'不包含'Form1_Load'的定义,并且没有可以找到接受类型'Detailed.Form1'的第一个参数的扩展方法'Form1_Load'(你是否缺少using指令或汇编引用?)

1 个答案:

答案 0 :(得分:1)

您想要的第一件事是提示用户输入目录的对话框。 因此,摆脱所有代码,启动一个新的项目获胜表单,并在表单中放置一个文本框,并在表单中放置一个按钮。

足够简单的一个文本框和一个按钮。现在,在“浏览”按钮的单击事件中,您编写代码以打开FolderBrowserDialog类的实例和.ShowDialog()。 要获得这条道路:

以下是屏幕输出示例:

enter image description here

代码非常简单,看看我的图片以及我必须执行多少代码。