zoukankan      html  css  js  c++  java
  • 若对File与Directory类陌生我们一起学习 Winform界面(技术改变世界cnblog)

    闲话不说了,要想学好 c# 文件和流是肯定要熟悉甚至精通的,第一天细读 File类与Directory,利用winform界面,和初学者一起交流心得。

    修为浅薄,如有错误,望指针,开源免费分享。

    需求:

    对于新手,我希望你先看下我的需求,然后自己考虑下,如何编写代码,然后再看我代码。

    1.注意 我右边使用的是ListBox 左边是textBox   要点:锻炼winform    思考下 textBox 的 textBox3.AppendText 方法与 textBox3.Text += "' 的区别?

    2.实现 按钮的 顺序使用 “必须按了某个按钮才可以激活下一个按钮”

    View Code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace FileOnlyStaticMethod
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    Directory.SetCurrentDirectory(@"c:\");//设置默认的路径
    listBox1.Items.Add("Directory类:");
    listBox1.Items.Add(@"SetCurrentDirectory方法:程序默认路径是c:\");
    textBox3.Text=("File类:\n");

    //
    button3.Enabled = false;
    button2.Enabled = false;


    }

    //CreatFile Button
    private void button1_Click(object sender, EventArgs e)
    {
    if (textBox1.Text == string.Empty)
    {
    MessageBox.Show("文件名不能为空");
    return;
    }
    if (File.Exists(textBox1.Text))
    {
    textBox3.AppendText(string.Format("Exists方法:({0})文件已存在\n", textBox1.Text));
    textBox1.Clear();
    return;
    }
    else
    {
    File.Create(textBox1.Text);
    textBox3.AppendText(string.Format("Creat方法:({0})文件已创建\n",textBox1.Text));
    textBox1.Clear();
    return;
    }
    }

    //CreatDirectoy Button
    private void button2_Click(object sender, EventArgs e)
    {
    listBox1.Items.Clear();
    if (textBox2.Text == string.Empty)
    {
    MessageBox.Show("目录不能为空");
    return;
    }
    if (Directory.Exists(textBox2.Text))
    {
    listBox1.Items.Add(string.Format("Exists方法:({0})文件已存在", textBox2.Text));
    }
    else
    {
    Directory.CreateDirectory(textBox2.Text);
    listBox1.Items.Add(string.Format("CreatDirectory方法:({0})文件已创建", textBox2.Text));
    }
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
    listBox1.Items.Clear();
    if (textBox2.Text == string.Empty)
    {
    MessageBox.Show("目录不能为空");
    return;
    }
    button3.Enabled = true;
    button2.Enabled = true;
    //string[] fils=Directory.GetFiles(textBox2.Text);//获得目录中的文件
    //foreach(var n in fils)
    //{
    // listBox1.Items.Add(n);
    //}
    //fils=Directory.GetDirectories(textBox2.Text);
    //foreach (var n in fils)//获得目录中的子目录
    //{
    // listBox1.Items.Add(n);
    //}
    string[] strs=Directory.GetFileSystemEntries(textBox2.Text);//获得目录中的子目录和文件
    foreach (var n in strs)
    {
    listBox1.Items.Add(n);
    }
    }

    private void button3_Click(object sender, EventArgs e)
    {
    if (listBox1.Items.Count == 0)
    {
    MessageBox.Show("请先选择要查询的目录");
    return;
    }
    else
    {
    string temp=Directory.GetCreationTime(listBox1.SelectedItem.ToString()).ToString();
    listBox1.Items.Clear();
    listBox1.Items.Add(temp);
    }
    }

    private void button2_Click_1(object sender, EventArgs e)
    {
    if (listBox1.Items.Count == 0)
    {
    MessageBox.Show("请先选择要查询的目录");
    return;
    }
    else
    {
    //listBox1.Items.Remove(listBox1.SelectedItem);这个的虽然也可以从列表删除,但是不是从磁盘删除
    Directory.Delete(listBox1.SelectedItem.ToString());//这个虽然删除了,但是不可以把列表内的内容更新
    listBox1.Items.Remove(listBox1.SelectedItem);
    }
    }

    private void button4_Click(object sender, EventArgs e)
    {
    if(textBox4.Text==string.Empty||textBox5.Text==string.Empty)
    {
    MessageBox.Show("不能为空");
    return;
    }
    File.Copy(textBox4.Text,textBox5.Text,true);
    }
    }
    }



  • 相关阅读:
    iOS开发之JSON格式数据的生成与解析
    Xcode 怎么查看代码总行数
    iOS OC与JS的交互(JavaScriptCore实现)
    Webstorm设置代码提示
    iOS app性能优化的那些事
    pThread多线程demo
    更新UI放在主线程的原因
    iOS小知识点
    上传本地代码及更新代码到GitHub教程
    logstash安装配置
  • 原文地址:https://www.cnblogs.com/IAmBetter/p/2322458.html
Copyright © 2011-2022 走看看