- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 VS2010 中将 C# 用于 WinForms 应用程序,我需要创建一个目录,其中的路径对于 .NET 方法来说太大(我相信 248 个字符限制),并遇到了来自谷歌的建议以使用 Unicode Win32 创建目录()。我最初尝试使用 Unicode 调用它并传递参数,但在几次尝试失败后,我减少了代码并使用了完全在这里找到的代码:
http://www.pinvoke.net/default.aspx/Structures/SECURITY_ATTRIBUTES.html
我仍然遇到同样的错误:
System.AccessViolationException 被捕获 Message=试图读取或写入 protected 内存。这通常表明其他内存已损坏。
不可否认,我对调用 Win32 函数一无所知,我真的只是在网上找到我能找到的东西并尝试学习。谁能告诉我我做错了什么?删除问题的非必要代码,我有:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Text;
namespace RFCGenerator
{
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}
public class RFC
{
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName, SECURITY_ATTRIBUTES lpSecurityAttributes);
protected void CopyDirectory(Uri Source, Uri Destination)
{
SECURITY_ATTRIBUTES lpSecurityAttributes = new SECURITY_ATTRIBUTES();
DirectorySecurity security = new DirectorySecurity();
lpSecurityAttributes.nLength = Marshal.SizeOf(lpSecurityAttributes);
byte[] src = security.GetSecurityDescriptorBinaryForm();
IntPtr dest = Marshal.AllocHGlobal(src.Length);
Marshal.Copy(src, 0, dest, src.Length);
lpSecurityAttributes.lpSecurityDescriptor = dest;
string path = @"C:\Test";
CreateDirectory(path, lpSecurityAttributes);
}
}
}
更新:根据 Hans 的建议,我确实让它在本地工作。但是,当我尝试使用 UNC 地址创建目录时,例如传入:
path = @"\\mydomain.com\foo\bar\newfolder"
我现在得到:
System.ComponentModel.Win32Exception 被捕获 Message=文件名、目录名或卷标语法不正确
我已验证\\mydomain.com\foo\bar\确实存在。
解决方案:
使用 Hans 的代码和一个小的修改来检查它是否是 UNC 路径(引用:http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx,在“最大路径长度限制”下):
string UnicodePath = (path.StartsWith(@"\\")) ? @"\\?\UNC\" + (path.Remove(0, 2)) : @"\\?\" + path;
if (!CreateDirectory(UnicodePath, IntPtr.Zero))
throw new System.ComponentModel.Win32Exception();
最佳答案
您没有使用 Unicode 版本,它需要 [DllImport] 声明中的 CharSet = CharSet.Unicode。此外,创建具有长名称的目录与安全属性无关。您必须在名称前加上 @"\\?\"
。因此:
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool CreateDirectory(string lpPathName, IntPtr lpSecurityAttributes);
...
if (!CreateDirectory(@"\\?\" + path, IntPtr.Zero))
throw new Win32Exception();
关于c# - 帮助在C#中使用PInvoke CreateDirectory(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3576657/
我正在 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
我是一名优秀的程序员,十分优秀!