嘿,我是初学者 [英] hey, i'm a beginner

查看:80
本文介绍了嘿,我是初学者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中创建文件以及如何读取文件属性?

How to create a file in C# and how can I read the file property?

推荐答案

您是初学者吗?所以呢?您听说过Google吗?

创建文件:

You''re a beginner? So what? Have you ever heard of google?

Create a file:

string path = @"c:\temp\MyTest.txt";
// Delete the file if it exists.
if (File.Exists(path))
{
    File.Delete(path);
}
// Create the file.
using (FileStream fs = File.Create(path, 1024))
{
    Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
    // Add some information to the file.
    fs.Write(info, 0, info.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(path))
{
    string s = "";
    while ((s = sr.ReadLine()) != null)
    {
        Console.WriteLine(s);
    }
}



获取文件的属性:



Get the file''s properties:

FileInfo oFileInfo = new FileInfo(filename);



只需查看FileInfo对象的属性,就可以获取有关文件的所有信息.



Just look at the properties of the FileInfo object, and you can get all info about a file.


要创建文件,请参见 ^ ].

要读取属性,请查看FileInfo 类.
请参见此处 [
To create a file, see here[^].

To read the properties, look at the FileInfo class.
See here[^].


这篇关于嘿,我是初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆