Python 官方文档:入门教程 => 点击学习
1. 案例学习:了解FileInfo类的一些主要属性 下面的示例演示了 FileInfo类的一些主要属性。 using System; using S
using System;
using System.io;
class Test
{
public static void Main()
{
string fileName = "C:\\autoexec.bat";
FileInfo fileInfo = new FileInfo(fileName);
if (!fileInfo.Exists)
{
return;
}
Console.WriteLine("{0} has a directoryName of {1}",fileName,fileInfo.DirectoryName);
}
} |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.windows.Forms;
using System.IO;
namespace FileOptionApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 复制文本文件
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
string somefile = @"C:\Documents and Settings\Administrator\My Documents\SQL Server2000安装故障.txt";
string target = @"c:\2.txt";
if (!File.Exists(somefile))
{
MessageBox.Show("文件不存在!");
}
else
{
if (File.Exists(target))
{
File.Delete(target);
}
File.Copy(somefile, target);
MessageBox.Show("文件复制成功!");
}
}
/// <summary>
/// 创建文本文件
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
string target = @"c:\2.txt";
if (File.Exists(target))
{
File.Delete(target);
}
File.CreateText(target);
}
/// <summary>
/// 删除文本文件
/// </summary>
private void button3_Click(object sender, EventArgs e)
{
string target = @"c:\2.txt";
if (File.Exists(target))
{
File.Delete(target);
MessageBox.Show("文件删除成功!");
}
}
}
} |
private void button1_Click(object sender, EventArgs e)
{
string path = @"C:\WINDOWS\IE4 Error Log.txt";
string target = @"c:\1.txt";
FileInfo myfile = new FileInfo(path);
if (!myfile.Exists)
{
MessageBox.Show("对不起,未发现路径文件!");
}
else
{
myfile.CopyTo(target);
MessageBox.Show("复制成功!");
}
} |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FileOptionApplication
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 获取文件信息单击事件
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
string somefile = @"C:\Documents and Settings\Administrator\My Documents\sql Server2000安装故障.txt";
FileInfo myfile = new FileInfo(somefile);
if (myfile.Exists)
{
MessageBox.Show("文件已经存在");
label1.Text = "文件创建时间:" + myfile.CreationTime.ToString();
label2.Text = "文件夹:" + myfile.Directory.ToString();
label3.Text = "文件夹名称:" + myfile.DirectoryName.ToString() + ",文件扩展名:" + myfile.Extension.ToString();
}
else
{
MessageBox.Show("文件并不存在");
}
}
}
} |
--结束END--
本文标题: 3-1 Fileinfo类的常用方法
本文链接: https://lsjlt.com/news/191033.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0