The UnicodeWideFilename example provides a code example for creating files with wide character filenames.
Classes
Class | Description | |
---|---|---|
Program |
Simple example to demonstrate reading and writing file paths with wide characters.
|
Examples
UnicodeWideFilename Sample Code
public class Program { private static readonly string _outputFilePath = "UnicodeWideFilename.txt"; /// <summary> /// Main entry point for UnicodeWideFilename. /// </summary> public static void Main() { try { byte[] origBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; string parentDir = Path.GetTempPath(); string fileName = "ヤマダ_タロウ_山田_太郎_やまだ^たろう.dcm"; string filePath = Path.Combine(parentDir, fileName); using (FileStream fs = FSUtil.WriteFileWithLongPath(filePath)) { fs.Write(origBytes, 0, 10); } // Read in the bytes written to disc byte[] result = new byte[10]; using (FileStream fs = FSUtil.ReadFileWithLongPath(filePath)) { fs.Read(result, 0, 10); } Debug.Assert(origBytes.Length == result.Length); StringBuilder sb = new StringBuilder(); sb.AppendFormat("FilePath: {1}{0}", Environment.NewLine, filePath); sb.AppendFormat("original bytes written: {0}", HexDump.ToHexString(origBytes)); sb.AppendFormat("Bytes read from file: {0}", HexDump.ToHexString(result)); WriteOutputToFile(_outputFilePath, sb.ToString()); Process.Start("notepad.exe", _outputFilePath); FSUtil.DeleteFile(filePath); } catch (Exception e) { Console.WriteLine("Exception caught during execution: {0}", e); } if (Debugger.IsAttached) { Console.Write("Press any key to continue . . . "); Console.ReadKey(); } } /// <summary> /// Write the given output text to disk in UTF8. /// </summary> /// <param name="outputFile">The output file path</param> /// <param name="outputText">The text to write</param> private static void WriteOutputToFile(string outputFile, string outputText) { using (StreamWriter writer = new StreamWriter(outputFile, false, Encoding.UTF8)) { writer.Write(outputText); } } }