C语言文件操作大全
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
al.Enqueue(fsi); } else
File.Copy(file.FullName, path + file.Name); } }
15.移动文件夹 /* using System.IO; using System.Collections; */
string filename = Path.GetFileName(%%1); string path=(%%2.LastIndexOf("\\") == %%2.Length - 1) ? %%2 : %%2 + "\\"; if (Path.GetPathRoot(%%1) == Path.GetPathRoot(%%2))
else {
foreach (string dir2 in Directory.GetDirectories(%%1)) {
string parent = Path.GetDirectoryName(dir2);
Directory.CreateDirectory(Path.Combine(%%2,
DirectoryInfo directory = temp as DirectoryInfo; Directory.CreateDirectory(path + directory.Name); foreach (FileSystemInfo fsi in directory.GetFileSystemInfos())
Queue<FileSystemInfo> al = new Queue<FileSystemInfo>(dirs.GetFileSystemInfos()); while (al.Count > 0) {
FileSystemInfo temp = al.Dequeue(); FileInfo file = temp as FileInfo; if (file == null) {
foreach (FileSystemInfo fi in d.GetFileSystemInfos()) {
Folders.Enqueue(fi); }
} else {
f.CopyTo(f.FullName.Replace(parent, path)); } }
14.复制目录下所有的文件夹到另一个文件夹下 /* using System.IO; using System.Collections; */ DirectoryInfo d = new DirectoryInfo(%%1); foreach (DirectoryInfo dirs in d.GetDirectories()) {
FileInfo f = folders[i] as FileInfo; if (f == null) {
DirectoryInfo d = folders[i] as DirectoryInfo; d.Delete(); } } }
6.清空文件夹 //using System.IO; Directory.Delete(%%1,true); Directory.CreateDirectory(%%1);
for(int i = 0; i < dataArray.Length; i++){ FileStream.WriteByte(dataArray[i]);
} // Set the stream position to the beginning of the file.
FileStream.Seek(0, SeekOrigin.Begin); // Read and verify the data.
Directory.Move(%%1, path + filename); else {
string parent = Path.GetDirectoryName(%%1);
Directory.CreateDirectory(path + Path.GetFileName(%%1));
DirectoryInfo dir = new DirectoryInfo((%%1.LastIndexOf("\\") ==
12.枚举一个文件夹中的所有文件夹 //using System.IO; foreach (string %%2 in Directory.GetDirectories(%%1)){
%%3 } /* DirectoryInfo dir = new DirectoryInfo(%%1); FileInfo[] files = dir.GetFiles("*.*"); foreach(FileInfo %%2 in files){
{
Folders.Enqueue(fi);
}
}
else
{
f.MoveTo(f.FullName.Replace(parent, path));
}
}
Directory.Delete(%%1, true);
}
16.移动目录下所有的文件夹到另一个目录下 /* using System.IO; using System.Collections; */ string filename = Path.GetFileName(%%1);
dpath
=
new
DirectoryInfo(d.FullName.Replace((parent.LastIndexOf("\\") == parent.Length - 1) ? parent :
parent + "\\", path));
dpath.Create();
foreach (FileSystemInfo fi in d.GetFileSystemInfos())
while (Folders.Count > 0)
{
FileSystemInfo tmp = Folders.Dequeue();
FileInfo f = tmp as FileInfo;
if (f == null)
{
DirectoryInfo d = tmp as DirectoryInfo;
DirectoryInfo
%%3 } */
13.复制文件夹 /* using System.IO; using System.Collections; */ string path = (%%2.LastIndexOf("\\") == %%2.Length - 1) ? %%2 : %%2+"\\"; string parent = Path.GetDirectoryName(%%1); Directory.CreateDirectory(path + Path.GetFileName(%%1)); DirectoryInfo dir = new DirectoryInfo((%%1.LastIndexOf("\\") == %%1.Length - 1) ? %%1 : %%1 + "\\"); FileSystemInfo[] fileArr = dir.GetFileSystemInfos(); Queue<FileSystemInfo> Folders = new Queue<FileSystemInfo>(dir.GetFileSystemInfos()); while (Folders.Count>0) {
%%2 } else{
%%3 }
11.写入属性 //using System.IO; FileInfo f = new FileInfo(%%1);
//设置只读 f.Attributes = myFile.Attributes | FileAttributes.ReadOnly; //设置可写 f.Attributes = myFile.Attributes & ~FileAttributes.ReadOnly;
9.写入随机文件 //using System.IO; byte[] dataArray = new byte[100000];//new Random().NextBytes(dataArray); using(FileStream FileStream = new FileStream(%%1, FileMode.Create)){ // Write the data to the file, byte by byte.
5.删Hale Waihona Puke Baidu一个目录下所有的文件夹 //using System.IO; foreach (string dirStr in Directory.GetDirectories(%%1)) {
DirectoryInfo dir = new DirectoryInfo(dirStr); ArrayList folders=new ArrayList(); FileSystemInfo[] fileArr = dir.GetFileSystemInfos(); for (int i = 0; i < folders.Count; i++) {
1.创建文件夹 //using System.IO; Directory.CreateDirectory(%%1);
2.创建文件 //using System.IO; File.Create(%%1);
3.删除文件 //using System.IO; File.Delete(%%1);
4.删除文件夹 //using System.IO; Directory.Delete(%%1);
%%1.Length - 1) ? %%1 : %%1 + "\\");
FileSystemInfo[] fileArr = dir.GetFileSystemInfos();
Queue<FileSystemInfo>
Folders
=
new
Queue<FileSystemInfo>(dir.GetFileSystemInfos());
for(int i = 0; i < FileStream.Length; i++){ if(dataArray[i] != FileStream.ReadByte()){ //写入数据错误 return; }
} //"数据流"+FileStream.Name+"已验证" }
10.读取文件属性 //using System.IO; FileInfo f = new FileInfo(%%1);//f.CreationTime,f.FullName if((f.Attributes & FileAttributes.ReadOnly) != 0){
7.读取文件 //using System.IO; StreamReader s = File.OpenText(%%1); string %%2 = null; while ((%%2 = s.ReadLine()) != null){
%%3 } s.Close();
8.写入文件 //using System.IO; FileInfo f = new FileInfo(%%1); StreamWriter w = f.CreateText(); w.WriteLine(%%2); w.Close();
FileSystemInfo tmp = Folders.Dequeue(); FileInfo f = tmp as FileInfo; if (f == null) {
DirectoryInfo d = tmp as DirectoryInfo;
Directory.CreateDirectory(d.FullName.Replace((parent.LastIndexOf("\\") == parent.Length - 1) ? parent : parent + "\\", path));
if (Path.GetPathRoot(%%1) == Path.GetPathRoot(%%2)) foreach (string dir in Directory.GetDirectories(%%1)) Directory.Move(dir, Path.Combine(%%2,filename));
Path.GetFileName(dir2)));
File.Copy(file.FullName, path + file.Name); } }
15.移动文件夹 /* using System.IO; using System.Collections; */
string filename = Path.GetFileName(%%1); string path=(%%2.LastIndexOf("\\") == %%2.Length - 1) ? %%2 : %%2 + "\\"; if (Path.GetPathRoot(%%1) == Path.GetPathRoot(%%2))
else {
foreach (string dir2 in Directory.GetDirectories(%%1)) {
string parent = Path.GetDirectoryName(dir2);
Directory.CreateDirectory(Path.Combine(%%2,
DirectoryInfo directory = temp as DirectoryInfo; Directory.CreateDirectory(path + directory.Name); foreach (FileSystemInfo fsi in directory.GetFileSystemInfos())
Queue<FileSystemInfo> al = new Queue<FileSystemInfo>(dirs.GetFileSystemInfos()); while (al.Count > 0) {
FileSystemInfo temp = al.Dequeue(); FileInfo file = temp as FileInfo; if (file == null) {
foreach (FileSystemInfo fi in d.GetFileSystemInfos()) {
Folders.Enqueue(fi); }
} else {
f.CopyTo(f.FullName.Replace(parent, path)); } }
14.复制目录下所有的文件夹到另一个文件夹下 /* using System.IO; using System.Collections; */ DirectoryInfo d = new DirectoryInfo(%%1); foreach (DirectoryInfo dirs in d.GetDirectories()) {
FileInfo f = folders[i] as FileInfo; if (f == null) {
DirectoryInfo d = folders[i] as DirectoryInfo; d.Delete(); } } }
6.清空文件夹 //using System.IO; Directory.Delete(%%1,true); Directory.CreateDirectory(%%1);
for(int i = 0; i < dataArray.Length; i++){ FileStream.WriteByte(dataArray[i]);
} // Set the stream position to the beginning of the file.
FileStream.Seek(0, SeekOrigin.Begin); // Read and verify the data.
Directory.Move(%%1, path + filename); else {
string parent = Path.GetDirectoryName(%%1);
Directory.CreateDirectory(path + Path.GetFileName(%%1));
DirectoryInfo dir = new DirectoryInfo((%%1.LastIndexOf("\\") ==
12.枚举一个文件夹中的所有文件夹 //using System.IO; foreach (string %%2 in Directory.GetDirectories(%%1)){
%%3 } /* DirectoryInfo dir = new DirectoryInfo(%%1); FileInfo[] files = dir.GetFiles("*.*"); foreach(FileInfo %%2 in files){
{
Folders.Enqueue(fi);
}
}
else
{
f.MoveTo(f.FullName.Replace(parent, path));
}
}
Directory.Delete(%%1, true);
}
16.移动目录下所有的文件夹到另一个目录下 /* using System.IO; using System.Collections; */ string filename = Path.GetFileName(%%1);
dpath
=
new
DirectoryInfo(d.FullName.Replace((parent.LastIndexOf("\\") == parent.Length - 1) ? parent :
parent + "\\", path));
dpath.Create();
foreach (FileSystemInfo fi in d.GetFileSystemInfos())
while (Folders.Count > 0)
{
FileSystemInfo tmp = Folders.Dequeue();
FileInfo f = tmp as FileInfo;
if (f == null)
{
DirectoryInfo d = tmp as DirectoryInfo;
DirectoryInfo
%%3 } */
13.复制文件夹 /* using System.IO; using System.Collections; */ string path = (%%2.LastIndexOf("\\") == %%2.Length - 1) ? %%2 : %%2+"\\"; string parent = Path.GetDirectoryName(%%1); Directory.CreateDirectory(path + Path.GetFileName(%%1)); DirectoryInfo dir = new DirectoryInfo((%%1.LastIndexOf("\\") == %%1.Length - 1) ? %%1 : %%1 + "\\"); FileSystemInfo[] fileArr = dir.GetFileSystemInfos(); Queue<FileSystemInfo> Folders = new Queue<FileSystemInfo>(dir.GetFileSystemInfos()); while (Folders.Count>0) {
%%2 } else{
%%3 }
11.写入属性 //using System.IO; FileInfo f = new FileInfo(%%1);
//设置只读 f.Attributes = myFile.Attributes | FileAttributes.ReadOnly; //设置可写 f.Attributes = myFile.Attributes & ~FileAttributes.ReadOnly;
9.写入随机文件 //using System.IO; byte[] dataArray = new byte[100000];//new Random().NextBytes(dataArray); using(FileStream FileStream = new FileStream(%%1, FileMode.Create)){ // Write the data to the file, byte by byte.
5.删Hale Waihona Puke Baidu一个目录下所有的文件夹 //using System.IO; foreach (string dirStr in Directory.GetDirectories(%%1)) {
DirectoryInfo dir = new DirectoryInfo(dirStr); ArrayList folders=new ArrayList(); FileSystemInfo[] fileArr = dir.GetFileSystemInfos(); for (int i = 0; i < folders.Count; i++) {
1.创建文件夹 //using System.IO; Directory.CreateDirectory(%%1);
2.创建文件 //using System.IO; File.Create(%%1);
3.删除文件 //using System.IO; File.Delete(%%1);
4.删除文件夹 //using System.IO; Directory.Delete(%%1);
%%1.Length - 1) ? %%1 : %%1 + "\\");
FileSystemInfo[] fileArr = dir.GetFileSystemInfos();
Queue<FileSystemInfo>
Folders
=
new
Queue<FileSystemInfo>(dir.GetFileSystemInfos());
for(int i = 0; i < FileStream.Length; i++){ if(dataArray[i] != FileStream.ReadByte()){ //写入数据错误 return; }
} //"数据流"+FileStream.Name+"已验证" }
10.读取文件属性 //using System.IO; FileInfo f = new FileInfo(%%1);//f.CreationTime,f.FullName if((f.Attributes & FileAttributes.ReadOnly) != 0){
7.读取文件 //using System.IO; StreamReader s = File.OpenText(%%1); string %%2 = null; while ((%%2 = s.ReadLine()) != null){
%%3 } s.Close();
8.写入文件 //using System.IO; FileInfo f = new FileInfo(%%1); StreamWriter w = f.CreateText(); w.WriteLine(%%2); w.Close();
FileSystemInfo tmp = Folders.Dequeue(); FileInfo f = tmp as FileInfo; if (f == null) {
DirectoryInfo d = tmp as DirectoryInfo;
Directory.CreateDirectory(d.FullName.Replace((parent.LastIndexOf("\\") == parent.Length - 1) ? parent : parent + "\\", path));
if (Path.GetPathRoot(%%1) == Path.GetPathRoot(%%2)) foreach (string dir in Directory.GetDirectories(%%1)) Directory.Move(dir, Path.Combine(%%2,filename));
Path.GetFileName(dir2)));