当前位置: 首页>>代码示例>>C#>>正文


C# Directory.Create方法代码示例

本文整理汇总了C#中System.IO.Directory.Create方法的典型用法代码示例。如果您正苦于以下问题:C# Directory.Create方法的具体用法?C# Directory.Create怎么用?C# Directory.Create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.Directory的用法示例。


在下文中一共展示了Directory.Create方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Create

        /// <summary>
        /// 	Creates a junction point from the specified directory to the specified target directory.
        /// </summary>
        /// <remarks>
        /// 	Only works on NTFS.
        /// </remarks>
        /// <param name = "junctionPoint">The junction point path</param>
        /// <param name = "targetDir">The target directory</param>
        /// <param name = "overwrite">If true overwrites an existing reparse point or empty directory</param>
        /// <exception cref = "IOException">Thrown when the junction point could not be created or when
        /// 	an existing directory was found and <paramref name = "overwrite" /> if false</exception>
        public static void Create(Directory junctionPoint, string targetDir, bool overwrite)
        {
            //targetDir = Path.GetFullPath(targetDir);

            if (junctionPoint.Exists())
            {
                if (!overwrite)
                    throw new IOException("Directory already exists and overwrite parameter is false.");
            }
            else
                junctionPoint.Create();

            using (var handle = OpenReparsePoint(junctionPoint.Path, EFileAccess.GenericWrite))
            {
                var targetDirBytes = Encoding.Unicode.GetBytes(NonInterpretedPathPrefix + Path.GetFullPath(targetDir));

                var reparseDataBuffer = new REPARSE_DATA_BUFFER();

                reparseDataBuffer.ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
                reparseDataBuffer.ReparseDataLength = (ushort) (targetDirBytes.Length + 12);
                reparseDataBuffer.SubstituteNameOffset = 0;
                reparseDataBuffer.SubstituteNameLength = (ushort) targetDirBytes.Length;
                reparseDataBuffer.PrintNameOffset = (ushort) (targetDirBytes.Length + 2);
                reparseDataBuffer.PrintNameLength = 0;
                reparseDataBuffer.PathBuffer = new byte[0x3ff0];
                Array.Copy(targetDirBytes, reparseDataBuffer.PathBuffer, targetDirBytes.Length);

                var inBufferSize = Marshal.SizeOf(reparseDataBuffer);
                var inBuffer = Marshal.AllocHGlobal(inBufferSize);

                try
                {
                    Marshal.StructureToPtr(reparseDataBuffer, inBuffer, false);

                    int bytesReturned;
                    var result = DeviceIoControl(handle.DangerousGetHandle(), FSCTL_SET_REPARSE_POINT,
                                                 inBuffer, targetDirBytes.Length + 20, IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);

                    if (!result)
                        ThrowLastWin32Error("Unable to create junction point.");
                }
                finally
                {
                    Marshal.FreeHGlobal(inBuffer);
                }
            }
        }
开发者ID:DotNetIO,项目名称:DotNetIO,代码行数:58,代码来源:JunctionPoint.cs


注:本文中的System.IO.Directory.Create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。