- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
只是想知道是否有 Directory.CreateDirectory()
的替代方法,因为我正在尝试创建一个超过 260 个字符的目录,虽然文件名不长,但目录路径是.
或
如果有任何技巧,我可以指向 CreateDirectory
在此位置创建一个文件夹,而无需提供目录的完整路径。当我在文件夹中创建文件夹等等时。必须有一些合法的方法来做到这一点。
我现在保存在隐藏标签中的字符串有问题,所以它不再是问题了。
最佳答案
是
的简单解决方案是使用启用 unc 的路径,
将
将允许最多约 32767 个字符的文件路径
string longPathEnabledFileName = Path.ToLongPath("C:\SomeVeryLongPath\....");
FileStream fs = new FileStream(longPathEnabledFileName);
这将简单地在路径前加上\\?\,告诉框架绕过 260 个字符的 MAX_PATH 限制。 不幸的是,在撰写本文时(从 4.0 版开始),\\?\的前缀在 .Net 中不支持
这给我们留下了 WinApi 解决方案和引用 Kernel32.dll 以使用 SafeFileHandle。来自 BCL 团队的 Kim Hamilton 已经在博客上发布了一系列解决 MAX_PATH 限制的方法(第 2 部分展示了如何使用 winapi 函数),其中包含一个代码片段以供引用:
// This code snippet is provided under the Microsoft Permissive License.using System;using System.IO;using System.Runtime.InteropServices;using Microsoft.Win32.SafeHandles;[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]internal static extern SafeFileHandle CreateFile( string lpFileName, EFileAccess dwDesiredAccess, EFileShare dwShareMode, IntPtr lpSecurityAttributes, ECreationDisposition dwCreationDisposition, EFileAttributes dwFlagsAndAttributes, IntPtr hTemplateFile);public static void TestCreateAndWrite(string fileName) { string formattedName = @"\\?\" + fileName; // Create a file with generic write access SafeFileHandle fileHandle = CreateFile(formattedName, EFileAccess.GenericWrite, EFileShare.None, IntPtr.Zero, ECreationDisposition.CreateAlways, 0, IntPtr.Zero); // Check for errors int lastWin32Error = Marshal.GetLastWin32Error(); if (fileHandle.IsInvalid) { throw new System.ComponentModel.Win32Exception(lastWin32Error); } // Pass the file handle to FileStream. FileStream will close the // handle using (FileStream fs = new FileStream(fileHandle, FileAccess.Write)) { fs.WriteByte(80); fs.WriteByte(81); fs.WriteByte(83); fs.WriteByte(84); }}
还有一个库将所有这些工作封装在谷歌代码中,称为 zeta long paths
关于c# - 替代 Directory.CreateDirectory(path) 支持长路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8755920/
我正在 Windows C# 应用程序下运行一个 dll。 APP调用dll api进行与系统的所有交易。当我尝试在 C# 应用程序级别创建目录时,它创建成功,但如果 dll 调用 createDir
所以我正在尝试创建一个目录,但我无法弄清楚为什么当我这样做时它会死掉。我已经尝试对路径进行硬编码测试,所以我认为这不是权限问题。当我切开要创建的路径时,我确保以 NULL 终止字符串。我什至尝试使用
我正在尝试使用 CreateDirectory 函数创建一个目录。但它似乎并不像我预期的那样工作。我的功能有什么问题? #include "stdafx.h" #include int _tmain(
我有一个 Winforms 应用程序,如果该目录不存在,它应该在 Public Documents 文件夹中创建一个子目录,并向其中保存一个文本文件。但是,如果该子目录不存在,它实际上会在“C:/Us
我在 VS2010 中将 C# 用于 WinForms 应用程序,我需要创建一个目录,其中的路径对于 .NET 方法来说太大(我相信 248 个字符限制),并遇到了来自谷歌的建议以使用 Unicode
我有服务器-客户端应用程序,它是一个文件管理器 我的问题是当我进入需要访问控制的文件夹(如系统文件夹)时,它变为只读,但我需要移动/删除或创建新文件夹,我怎样才能获得这样做的权限? 这是我在服务器端创
我创建了这样的方法: Flux download(String bucket, List s3FileNames) { String tmpDirName = UUID.randomUUID(
我有以下代码来为我生成的文件准备一个文件夹: if (Directory.Exists(outputDir)) { Directory.Delete(outputDir, true); } D
我是 C++ 新手。我面临的问题是 CreateDirectory 方法。 CreateDirectory("\\ServerName\foldername\",NULL) 没有错误,但它也没有创建任
我正在使用 C# 2010 创建一个应用程序,其目的是复制具有指定扩展名的文件。它使用 .NET Framework 3.5。 我有以下代码: private void GetFiles()
我遇到的问题是我的路径字符串通过了 Path.GetInvalidPathChars() 检查,但在尝试创建目录时失败了。 static void Main(string[] args) {
我们有一个完整路径的列表,我们要将一些文件复制到其中,有点像这样 C:\temp\sub1\file1A C:\temp\sub1\file1B C:\temp\sub2\file2A C:\
我正在使用 Directory.CreateDirectory("\\\\MyShared\\Pics\\NewFolder")在目录 \\MyShared\Pics 中创建一个新文件夹我得到一个异常
我在使用 Java 7 的 Files 类时遇到了一个看似奇怪的问题。我想在开始编写之前确保我的目录和文件存在以避免 FileNotFoundException,并且根据 Javadocs , cre
我正在尝试创建一个远程目录,然后向其中写入一个文件。每隔一段时间,应用程序在尝试写入文件时会失败并出现 System.IO.DirectoryNotFoundException。 当我写入文件时,我使
我有一个用于创建目录的函数。它使用 CreateDirectoryA() CreateDirectory 报告失败,但是当我使用 GetLastError() 检查错误代码时,它报告 ERROR_SU
我对以下情况感到困惑。我的应用程序尝试查找指定目录: HANDLE _dh, _fh; // Handles for a files _dh = CreateFile(_ddn, GENERIC_RE
我有一个 C# 应用程序,它可以在许多 PC、笔记本电脑上正常运行。但是,我复制到我客户的电脑(4TB HDD - windows 10 家庭版),我的应用程序停止工作! 我尝试将 MessageBo
我在路径 C:\Users\MYUSER\Desktop\TEST\ 上创建了一个文件夹。 我有以下代码: private const string DIR = @"C:\Users\MYUSER\D
我的程序中有以下代码行,这些代码取自 Stephen G. Kochan 的第四版《Objective-C 编程》: [fileManager createDirectoryAtPath:DATABA
我是一名优秀的程序员,十分优秀!