LAUREL BRIDGE

LaurelBridge.DCFExamples.UnicodeLongFilename Namespace

DICOM Connectivity Framework V3.4
The UnicodeLongFilename example provides a code example for creating files with long filenames.
Classes

  ClassDescription
Public classProgram
Simple example to demonstrate reading and writing long filenames on Windows.
Remarks

Supported OS Platforms:

  • Windows - .Net Framework 4.7.2 64-bit and 32-bit
  • Windows - .Net Core 2.1 64-bit and 32-bit

Examples

UnicodeLongFilename Sample Code
public class Program
{
    /// <summary>
    /// Main entry point for UnicodeLongFilename.
    /// </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 dirPath = CreateTmpDirPath(parentDir);
            string fileName = Path.GetFileName(Path.GetTempFileName());

            // Write the original byte array to disc using the long file path
            string filePath = Path.Combine(dirPath, fileName);
            Debug.Assert(filePath.Length > 260);
            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);
            Console.WriteLine("FilePath: {1}{0}", Environment.NewLine, filePath);
            Console.WriteLine("original bytes written: {0}", HexDump.ToHexString(origBytes));
            Console.WriteLine("Bytes read from file:   {0}", HexDump.ToHexString(result));

            FSUtil.DeleteFile(filePath);
            CleanupDirectories(dirPath);

        }
        catch (Exception e)
        {
            Console.WriteLine("Exception caught during execution: {0}", e);
            Environment.ExitCode = 1;
        }

        if (Debugger.IsAttached)
        {
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey();
        }
    }

    /// <summary>
    /// Create a directory path greater than 260 characters in the users TMP directory
    /// </summary>
    /// <returns>the path of the directory created</returns>
    private static string CreateTmpDirPath(string parentDir)
    {
        // Create a sub directory 100 characters long
        StringBuilder tmp = new StringBuilder();
        for (int j = 0; j < 100; j++) { tmp.Append(0); }
        string subDir = tmp.ToString();

        // Build a long directory name, creating each dir as we go
        StringBuilder sb = new StringBuilder();
        sb.Append(parentDir);
        for (int i = 0; i < 3; i++)
        {
            sb.Append(subDir);
            sb.Append("\\");
            FSUtil.CreateDirectory(sb.ToString());
        }

        return sb.ToString();
    }

    /// <summary>
    /// Cleanup any directories created in the parent directory, knowing that we hard code the 
    /// length of each directory at 100 characters. Note that removeDirectory will not remove
    ///  a directory if it is not empty.
    /// </summary>
    /// <param name="dirPath">File path of the directories to cleanup.</param>
    private static void CleanupDirectories(string dirPath)
    {
        string dir = dirPath;
        FSUtil.RemoveDirectory(dir);

        for (int i = 0; i < 2; i++)
        {
            dir = dir.Remove(dir.Length - 101, 101);
            FSUtil.RemoveDirectory(dir);
        }
    }
}