谁能提供“FileINFO”的详细用法 包括属性、 方法!!(肯定给分)

scc1980 2005-11-09 08:09:32
如题!
...全文
499 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangxiqi 2005-11-14
  • 打赏
  • 举报
回复
uo
scc1980 2005-11-10
  • 打赏
  • 举报
回复
狂谢!
ASP0000 2005-11-10
  • 打赏
  • 举报
回复
[C#]
using System;
using System.IO;

public class FileInfoMainTest
{
public static void Main()
{
// Open an existing file, or create a new one.
FileInfo fi = new FileInfo("temp.txt");
// Create a writer, ready to add entries to the file.
StreamWriter sw = fi.AppendText();
sw.WriteLine("This is a new entry to add to the file");
sw.WriteLine("This is yet another line to add...");
sw.Flush();
sw.Close();
// Get the information out of the file and display it.
StreamReader sr = new StreamReader( fi.OpenRead() );
while (sr.Peek() != -1)
Console.WriteLine( sr.ReadLine() );
}
}

[C++]
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
// Open an existing file, or create a new one.
FileInfo* fi = new FileInfo(S"temp.txt");
// Create a writer, ready to add entries to the file.
StreamWriter* sw = fi->AppendText();
sw->WriteLine(S"This is a new entry to add to the file");
sw->WriteLine(S"This is yet another line to add...");
sw->Flush();
sw->Close();
// Get the information out of the file and display it.
StreamReader* sr = new StreamReader(fi->OpenRead());
while (sr->Peek() != -1)
Console::WriteLine(sr->ReadLine());
}

[JScript]
import System;
import System.IO;

public class FileInfoMainTest {
public static function Main() : void {

// Open an existing file, or create a new one.
var fi : FileInfo = new FileInfo("temp.txt");
// Create a writer, ready to add entries to the file.
var sw : StreamWriter = fi.AppendText();
sw.WriteLine("This is a new entry to add to the file");
sw.WriteLine("This is yet another line to add...");
sw.Flush();
sw.Close();
// Get the information out of the file and display it.
var sr : StreamReader = new StreamReader( fi.OpenRead() );
while (sr.Peek() != -1)
Console.WriteLine( sr.ReadLine() );
}
}
FileInfoMainTest.Main();

ASP0000 2005-11-10
  • 打赏
  • 举报
回复
初始化 FileInfo 类的新实例,它作为文件路径的包装。

[Visual Basic]
Public Sub New( _
ByVal fileName As String _
)
[C#]
public FileInfo(
string fileName
);
[C++]
public: FileInfo(
String* fileName
);
[JScript]
public function FileInfo(
fileName : String
);
参数
fileName
新文件的完全限定名或相对文件名。
异常
异常类型 条件
ArgumentNullException fileName 为空引用(Visual Basic 中为 Nothing)。
SecurityException 调用方没有所要求的权限。
ArgumentException 文件名为空,只包含空白,或包含无效字符。
UnauthorizedAccessException 对 fileName 的访问被拒绝。
PathTooLongException 指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。
NotSupportedException fileName 字符串中间有一个冒号 (:)。

备注
可以指定完全限定文件名或相对文件名,但是安全检查获取完全限定名。

有关使用此方法的示例,请参见下面的“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作... 请参见本主题中的示例...
创建文本文件。 向文件写入文本
写入文本文件。 向文件写入文本
读取文本文件。 从文件读取文本
向文件中追加文本。 打开并附加到日志文件
File.AppendText

FileInfo.AppendText

重命名或移动文件。 File.Move
FileInfo.MoveTo

删除文件。 File.Delete
FileInfo.Delete

复制文件。 File.Copy
FileInfo.CopyTo

获取文件大小。 FileInfo.Length
获取文件属性。 File.GetAttributes
设置文件属性。 File.SetAttributes
确定文件是否存在。 File.Exists
读取二进制文件。 对刚创建的数据文件进行读取和写入
写入二进制文件。 对刚创建的数据文件进行读取和写入
检索文件扩展名。 Path.GetExtension
检索文件的完全限定路径。 Path.GetFullPath
检索路径中的文件名和扩展名。 Path.GetFileName
更改文件扩展名。 Path.ChangeExtension

示例
下面的示例使用此构造函数创建两个文件,并接着对其进行写入、读取、复制和删除操作。

[Visual Basic]
Imports System
Imports System.IO

Class Test

Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim fi1 As FileInfo = New FileInfo(path)

If fi1.Exists = False Then
'Create a file to write to.
Dim sw As StreamWriter = fi1.CreateText()
sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
sw.Flush()
sw.Close()
End If

'Open the file to read from.
Dim sr As StreamReader = fi1.OpenText()

Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop

Try
Dim path2 As String = path + "temp"
Dim fi2 As FileInfo = New FileInfo(path2)

'Ensure that the target does not exist.
fi2.Delete()

'Copy the file.
fi1.CopyTo(path2)
Console.WriteLine("{0} was copied to {1}.", path, path2)

'Delete the newly created file.
fi2.Delete()
Console.WriteLine("{0} was successfully deleted.", path2)

Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
qidizi 2005-11-10
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemiofileinfoclasstopic.asp

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧