【1】在指定路径下创建文件夹
if ( ! Directory. Exists ( @"C:\Users\USER\Desktop\测试文件夹\A" ) )
{
Directory. CreateDirectory ( @"C:\Users\USER\Desktop\测试文件夹\A" ) ;
print ( "文件夹不存在,创建" ) ;
}
【2】根据文件路径删除文件
if ( File. Exists ( @"C:\Users\USER\Desktop\测试文件夹\a.txt" ) )
{
File. Delete ( @"C:\Users\USER\Desktop\测试文件夹\a.txt" ) ;
print ( "查找到文件并删除" ) ;
}
【3】加载文件夹中的图片
【3-1】WWW类加载
Texture2D texture;
public GameObject cube;
void Start ( )
{
StartCoroutine ( LoadTexture ( ) ) ;
cube. GetComponent < MeshRenderer > ( ) . material. mainTexture = texture;
}
IEnumerator LoadTexture ( )
{
string str = @"file:///C:\Users\USER\Desktop\a.jpg" ;
WWW www = new WWW ( str) ;
texture = www. texture;
yield return texture;
}
【3-2】 文件流加载
public GameObject cube;
void Start ( )
{
string imagePath = @"C:\Users\USER\Desktop\a.jpg" ;
FileStream files = new FileStream ( imagePath, FileMode. Open) ;
byte [ ] imgByte = new byte [ files. Length] ; files. Read ( imgByte, 0 , imgByte. Length) ;
files. Close ( ) ;
Texture2D newtexture = new Texture2D ( 100 , 100 ) ;
newtexture. LoadImage ( imgByte) ;
cube. GetComponent < MeshRenderer > ( ) . material. mainTexture = newtexture;
}
【4】将项目中图片复制到指定文件夹中
Texture2D texture;
void Start ( )
{
StartCoroutine ( LoadTexture ( ) ) ;
byte [ ] imagebytes = texture. EncodeToJPG ( ) ;
string newName= "新命名" ;
string savePath = @"C:\Users\USER\Desktop\测试文件夹" ;
string dirPath = savePath + newName;
File. WriteAllBytes ( dirPath, imagebytes) ;
}
IEnumerator LoadTexture ( )
{
string str = @"file:///C:\Users\USER\Desktop\a.jpg" ;
WWW www = new WWW ( str) ;
texture = www. texture;
yield return texture;
}
【5】将文件夹中某个文件复制到另一文件夹中
string fileName= "新建文本文档.txt" ;
string fromDirPath = @"C:\Users\USER\Desktop\A" ;
string targetDirPath= @"C:\Users\USER\Desktop\B" ;
void Start ( )
{
CopyDirectory ( fromDirPath, targetDirPath, fileName) ;
}
public static void CopyDirectory ( string fromDir, string targetDir, string fileName)
{
if ( ! File. Exists ( targetDir + "/" + fileName) )
{
File. Copy ( fromDir + "/" + fileName, Path. Combine ( targetDir, fileName) , true ) ;
}
else
{
Debug. Log ( "文件夹中已包含此文件,请手动复制。路径为:" + targetDir + "/" + fileName) ;
}
}
【6】读取文件夹中文件个数
string dirPath = @"C:\Users\USER\Desktop\测试文件夹" ;
if ( ! Directory. Exists ( dirPath) )
{
return ;
}
int number = 0 ;
DirectoryInfo folder = new DirectoryInfo ( dirPath) ;
foreach ( FileInfo fi in folder. GetFiles ( ) )
{
number++ ;
}
print ( number) ;
【7】读取文件夹下所有文件名
string dirPath = @"C:\Users\USER\Desktop\测试文件夹" ;
DirectoryInfo dir = new DirectoryInfo ( dirPath) ;
FileInfo[ ] files = dir. GetFiles ( ) ;
foreach ( var file in files)
{
print ( file. Name) ;
}
【8】读取文件夹下所有文件路径
string dirPath = @"C:\Users\USER\Desktop\测试文件夹" ;
string [ ] dirs = Directory. GetFiles ( dirPath) ;
for ( int j = 0 ; j < dirs. Length; j++ )
{
print ( dirs[ j] ) ;
}
【9】Unity对Txt文件的读写操作
void WriteTxt ( string info)
{
StreamWriter sw;
FileInfo fi = new FileInfo ( txt文件路径) ;
sw = fi. CreateText ( ) ;
sw. Write ( info) ;
sw. Close ( ) ;
sw. Dispose ( ) ;
}
void ReadTxt ( )
{
StreamReader sr = null ;
sr = File. OpenText ( txt文件路径) ;
sr. Close ( ) ;
sr. Dispose ( ) ;
}