????? ????? ?????? ?????/????? ????? ???????? ?? Visual C# 2005 Express Edition

?????? ????????? ?????? ?????????
???? ???????: 910356 - ??? ???????? ???? ????? ????? ??? ???????.
?????? ??? ????? Microsoft Visual Basic .NET ?? ??? ???????? ???? 304427.
?????? ??? ????? Microsoft Visual C++ .NET ?? ??? ???????? ???? 307398.
?????? ??? ????? Microsoft Visual C# .NET ?? ??? ???????? ???? 304430.
???? ??? ??????? ??? ????? ????? Microsoft .NET Framework ??? ????? System.IO.
????? ???? | ?? ????

?? ??? ??????

????? ???

???? ??? ??????? ???? ????? ??? ????? ????? ?????? ?????/????? ????? ??????? ?? Microsoft Visual C# 2005 Express Edition. ??? ??? ???????? ?????? ?? Microsoft .NET Framework ???? ???? ????? ?????? ?? ?????? ????? ?? Microsoft .NET ??? ???? ???? FileSystemObject ??????? ?? ?????? ?? ????? Microsoft Visual Studio 6.0. ???? ???????? ???? ????? ????? ??????? ?? ??? ??????? ??? ????? ????? ???????:
186118????? ??????? FileSystemObject ?? Visual Basic
??? ???? ??????? ??????? ?????? FileSystemObject ?? .NET. ??? ???? FileSystemObject ??? ???? "???? ???? ???? (COM) ? ????? .NET ?????? ??? ?????? FileSystemObject ???? ???? ??????? ??????? ????????. ??? ??? ???? ??????? ????? ???? .NET ????? ?????? ??. ??? ????? ?? ???? ???? ?????? ???????? ??????? ?? .NET Framework ????? ??? ?????? ??? ??????? ???? FileSystemObject ???? ????? ???? ??????? ??????? ????????:
  • ??? ???
  • ??? FileInfo
  • ??? ??????
  • ??? DirectoryInfo
  • ?????? ?????? ??? ?????

???????

???? ??????? ??????? ??????? ?????? ??? ????? ?????? ???????? ?????? ? ??? ??????? ???? ???????:
  • Microsoft Visual C# 2005 Express Edition ?? Microsoft Visual Studio 2005

?????? ?????/????? ????? demonstrated

??? ????? ????????? ???????? ?? ??? ??????? ????? ????? ?????? ?????/????? ????? ????????. ??? ????? "?????? Step-by-step" ????? ????? ????? ?????? ???? ?????? ?????/????? ????? ??????:
  • ????? ??? ???.
  • ????? ??? ???.
  • ??? ??????? ?????.
  • ????? ?????? ???????.
  • ????? ????????.
  • ??? ???????.
?????? ??? ??? ???? ?? ??????? ????? ????????? ???????? ??????? ?????? ??? ?????? ?? ???:
  • ??? ?? ????? ????? ??? System.IO ??? ???.
    using System.IO;
  • ??? ????? ????? winDir ??? ???.
    string    winDir=System.Environment.GetEnvironmentVariable("windir");
  • ??? ?? ???? ?????? ?????? addListItem ??? ???.
    private void addListItem(string value)
    {
    	this.listbox1.Items.Add(value);
    }
    ?????? ????? ?? ??????? ???????? ?????? addListItem ????? ??????? ??????? ??????? ??????.
    this.listbox1.Items.Add(value);

????? ??? ???

?????? ?????? ????????? ???????? ?????? ??? StreamReader ????? ????? System.ini. ??? ????? ??????? ????? ??? ???? ???? ListBox. ??? ??????? ???? ???? ???????? ?????? ??????? ??? ??? ????? ????. ???? ???? ??? ??? ?????? ??? ?????? ??? ????? ?????. ?????? ??? ?????? ??? ????? ??????? ???? ????? ?????? ??? ????? it.
StreamReader reader = new StreamReader(winDir + "\\system.ini");
            try
            {
                do
                {
                    addListItem(reader.ReadLine());
                }
                while (reader.Peek() != -1);
            }

            catch
            {
                addListItem("File is empty.");
            }

            finally
            {
                reader.Close();
            }

????? ??? ???

?????? ?????? ?????? ?? ????????? ???????? ??? StreamWriter ?????? ??? ??????? ??? ???. ??? ??? ???? ??? ????? ????? ??? ????? ??????? ?? ????? ???
StreamWriter writer = new StreamWriter("c:\\KBTest.txt");
            writer.WriteLine("File created by using StreamWriter class.");
            writer.Close();
            this.listBox1.Items.Clear();
            addListItem("File written to C:\\KBTest.txt");

??? ??????? ?????

?????? ?????? ????????? ???????? ??????? ???? FileInfo ?????? ??? ????? ???. ??? ??????? ????? Notepad.exe ?? ??? ?????? ???. ???? ??????? ?? ???? ListBox.
FileInfo FileProps = new FileInfo(winDir + "\\notepad.exe");
            addListItem("File Name = " + FileProps.FullName);
            addListItem("Creation Time = " + FileProps.CreationTime);
            addListItem("Last Access Time = " + FileProps.LastAccessTime);
            addListItem("Last Write Time = " + FileProps.LastWriteTime);
            addListItem("Size = " + FileProps.Length);
            FileProps = null;

????? ?????? ???????

?????? ?????? ????????? ???????? ?????? ??? ?????? ? ????? ???? ??????? ????? ?????? ????? ?????? ??? ????. ??? ???? ??????? ??? ???? ??????? ?? ???? ListBox.
string[] drives = Directory.GetLogicalDrives();
    foreach(string drive in drives)
    {
        addListItem(drive);
    }

????? ????????

?????? ?????? ????????? ???????? ?????? ????? GetDirectories ??? ?????? ???????? ????? ????????.
string[] dirs = Directory.GetDirectories(winDir);
    foreach(string dir in dirs)
        {
            addListItem(dir);
        }

??? ???????

?????? ?????? ????????? ???????? ?????? ????? GetFiles ??? ?????? ???????? ????? ?? ???????.
string[] files = Directory.GetFiles(winDir);
            foreach (string fileName in files)
            {
                addListItem(fileName);
            }
?? ???? ?????? ?????? ????? ????? ?????????? ?????? ??? ???????. ??????? ??? ????? ?? ?? ???? ??????? ?????????. ?????? ?????????? ?? ?? ???? ???? ???????? ??? ??????? ?? ???????? ???? ????? ?????? ????. ??? ?????? ??? ?????????? ??? ????? ????????? ???????? ??? ?????? ???? ??????? ??????????? ???? ?? ??? ?????.

?????? ???? ?????

?????? ????? ?????? ???? ?????? ?????/????? ????? ???????? ???? ??????? ???????:
  1. ?? Visual C# 2005 Express Edition ?? ?? Visual Studio 2005 ????? ????? Visual C# Windows ???? ???? WindowsApplication1. ?????????? ??? ????? ????? ?????? Form1.
  2. ?? "?????? ??????" ???? ??? ?????? ?????? ??? Form1.cs ??? ?? ???? ??? ??? ????????? ????????.
  3. ??? ???? ????????? ???????? ???????? ?? "???? ????????? ????????.
  4. ?? ???? ???????? ???????? ??????? ?? "???? ????????? ????????.
    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.button1.Text = "Read Text File";
                this.button2.Text = "Write Text File";
                this.button3.Text = "View File Information";
                this.button4.Text = "List Drives";
                this.button5.Text = "List Subfolders";
                this.button6.Text = "List Files";
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                // How to read a text file.
                // try...catch is to deal with a file that has a length of 0 bytes.
                this.listBox1.Items.Clear();
                StreamReader reader = new StreamReader(winDir + "\\system.ini");
                try
                {
                    do
                    {
                        addListItem(reader.ReadLine());
                    }
                    while (reader.Peek() != -1);
                }
    
                catch
                {
                    addListItem("File is empty.");
                }
    
                finally
                {
                    reader.Close();
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                // How to create and write to a text file.
                StreamWriter writer = new StreamWriter("c:\\KBTest.txt");
                writer.WriteLine("File created by using StreamWriter class.");
                writer.Close();
                this.listBox1.Items.Clear();
                addListItem("File written to C:\\KBTest.txt");
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                // How to retrieve file properties (example uses Notepad.exe).
                this.listBox1.Items.Clear();
                FileInfo FileProps = new FileInfo(winDir + "\\notepad.exe");
                addListItem("File Name = " + FileProps.FullName);
                addListItem("Creation Time = " + FileProps.CreationTime);
                addListItem("Last Access Time = " + FileProps.LastAccessTime);
                addListItem("Last Write Time = " + FileProps.LastWriteTime);
                addListItem("Size = " + FileProps.Length);
                FileProps = null;
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                // How to obtain a list of disk drives.
                this.listBox1.Items.Clear();
                string[] drives = Directory.GetLogicalDrives();
                foreach (string drive in drives)
                {
                    addListItem(drive);
                }
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                // How to obtain a list of folders.
                // Example uses Windows folder.
                this.listBox1.Items.Clear();
                string[] dirs = Directory.GetDirectories(winDir);
                foreach (string dir in dirs)
                {
                    addListItem(dir);
                }
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                // How to obtain a list of files.
                // Example uses Windows folder.
                this.listBox1.Items.Clear();
                string[] files = Directory.GetFiles(winDir);
                foreach (string fileName in files)
                {
                    addListItem(fileName);
                }
            }
        }
    }
  5. ?? "?????? ??????" ???? ??? ?????? ?????? ??? Form1.Designer.cs ??? ?? ???? ??? ??? ????????? ????????.
  6. ??? ???? ????????? ???????? ???????? ?? "???? ????????? ????????.
  7. ?? ???? ???????? ???????? ??????? ?? "???? ????????? ????????. ????? ?????
    namespace WindowsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            private string winDir = System.Environment.GetEnvironmentVariable("windir");
    
            /// <summary>
            /// Clean up any resources that are 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);
            }
    
            private void addListItem(string value)
            {
                this.listBox1.Items.Add(value);
            }
    
            #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.listBox1 = new System.Windows.Forms.ListBox();
                this.button1 = new System.Windows.Forms.Button();
                this.button2 = new System.Windows.Forms.Button();
                this.button3 = new System.Windows.Forms.Button();
                this.button4 = new System.Windows.Forms.Button();
                this.button5 = new System.Windows.Forms.Button();
                this.button6 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // listBox1
                // 
                this.listBox1.FormattingEnabled = true;
                this.listBox1.Location = new System.Drawing.Point(23, 24);
                this.listBox1.Name = "listBox1";
                this.listBox1.Size = new System.Drawing.Size(525, 251);
                this.listBox1.TabIndex = 0;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(23, 290);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(171, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(23, 319);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(171, 23);
                this.button2.TabIndex = 2;
                this.button2.Text = "button2";
                this.button2.UseVisualStyleBackColor = true;
                this.button2.Click += new System.EventHandler(this.button2_Click);
                // 
                // button3
                // 
                this.button3.Location = new System.Drawing.Point(200, 290);
                this.button3.Name = "button3";
                this.button3.Size = new System.Drawing.Size(171, 23);
                this.button3.TabIndex = 3;
                this.button3.Text = "button3";
                this.button3.UseVisualStyleBackColor = true;
                this.button3.Click += new System.EventHandler(this.button3_Click);
                // 
                // button4
                // 
                this.button4.Location = new System.Drawing.Point(200, 319);
                this.button4.Name = "button4";
                this.button4.Size = new System.Drawing.Size(171, 23);
                this.button4.TabIndex = 4;
                this.button4.Text = "button4";
                this.button4.UseVisualStyleBackColor = true;
                this.button4.Click += new System.EventHandler(this.button4_Click);
                // 
                // button5
                // 
                this.button5.Location = new System.Drawing.Point(377, 290);
                this.button5.Name = "button5";
                this.button5.Size = new System.Drawing.Size(171, 23);
                this.button5.TabIndex = 5;
                this.button5.Text = "button5";
                this.button5.UseVisualStyleBackColor = true;
                this.button5.Click += new System.EventHandler(this.button5_Click);
                // 
                // button6
                // 
                this.button6.Location = new System.Drawing.Point(377, 319);
                this.button6.Name = "button6";
                this.button6.Size = new System.Drawing.Size(171, 23);
                this.button6.TabIndex = 6;
                this.button6.Text = "button6";
                this.button6.UseVisualStyleBackColor = true;
                this.button6.Click += new System.EventHandler(this.button6_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(580, 400);
                this.Controls.Add(this.button6);
                this.Controls.Add(this.button5);
                this.Controls.Add(this.button4);
                this.Controls.Add(this.button3);
                this.Controls.Add(this.button2);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.listBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.ListBox listBox1;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.Button button3;
            private System.Windows.Forms.Button button4;
            private System.Windows.Forms.Button button5;
            private System.Windows.Forms.Button button6;
        }
    }
  8. ???? F5 ?????? ??????? ???????.
  9. ???? ??? ??????? ???? ??????? ??????.
?????? ???? ???????? Visual C# 2005 Express Edition ?? Visual Studio 2005 ????? ????? ??? ??????? ??? ????? ?????? ????? ????? Windows. ????? ??????? Form1. ??? ????? ??????? ???? ???? ??????? ??? Form1.cs ???? Form1.designer.cs. ????? ????????? ???????? ?? ??? Form1.cs. ???? ????? Windows ?????? ???????? ???????? ?? ??? Form1.designer.cs.

?????? ??? ???? ?? ????????? ??? ???? ????? Windows ?? Visual C# 2005 Express Edition ?? ?? Visual Studio 2005 ?? ?????? ???? ???? ????? Microsoft (MSDN) ?????? ??? ?????:
http://msdn2.microsoft.com/en-us/library/ms173077.aspx

???????

???? ???????: 910356 - ????? ??? ??????: 20/?? ??????/1427 - ??????: 1.3
????? ???
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Express Edition
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual Studio 2005 Team System Architect Edition
  • Microsoft Visual Studio 2005 Team System Developer Edition
  • Microsoft Visual Studio 2005 Team System Team Foundation:
  • Microsoft Visual Studio 2005 Team System Test Edition
????? ??????: 
kbmt kbvs2005std kbvs2005mos kbvs2005exp kbvs2005pro kbvs2005search kbio kbhowtomaster kbsample kbstepbystep kbcodesnippet kbcode kbhowto KB910356 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????910356

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com