28 lines
870 B
C#
28 lines
870 B
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace Cryville.Common {
|
|
public static class IOExtensions {
|
|
public static DirectoryInfo GetSubdirectory(this DirectoryInfo dir, string name) {
|
|
var l1 = dir.GetDirectories(name);
|
|
if (l1.Length == 0) return dir.CreateSubdirectory(name);
|
|
else return l1[0];
|
|
}
|
|
|
|
public static string ReadUInt16String(this BinaryReader reader, Encoding encoding = null) {
|
|
if (encoding == null) encoding = Encoding.UTF8;
|
|
var len = reader.ReadUInt16();
|
|
byte[] buffer = reader.ReadBytes(len);
|
|
return encoding.GetString(buffer);
|
|
}
|
|
|
|
public static void WriteUInt16String(this BinaryWriter writer, string value, Encoding encoding = null) {
|
|
if (encoding == null) encoding = Encoding.UTF8;
|
|
byte[] buffer = encoding.GetBytes(value);
|
|
writer.Write((ushort)buffer.Length);
|
|
writer.Write(buffer);
|
|
}
|
|
}
|
|
}
|