Is there a Kernel32 API for different System.IO API"S
In my application we have requirement where in we have to do System.IO operation on depth longer than 256 characters an开发者_Python百科d in this scenario all the System.IO API's are failing. We are using below API's.
- System.IO.Path.Combine()
- System.IO.Path.GetDirectoryName()
- System.IO.Path.GetFileName()
- System.IO.Path.GetPathRoot()
- System.IO.Directory.Exists()
- System.IO.Directory.GetFiles()
- System.IO.Directory.GetDirectories()
- System.IO.Directory.CreateDirectory()
Please guide me, if there is any substitute of the above API's available which will work for more than 256 characters,
Thank You
There's a great series on the BCL Team Blog regarding long paths:
- Long Paths in .NET, Part 1 of 3
- Long Paths in .NET, Part 2 of 3
- Long Paths in .NET, Part 3 of 3
- Long Paths in .NET, Part 3 of 3 Redux
It's littered with examples of how to use the Win32 API to handle long filenames, as well as plenty of explanation of the problems and pitfalls you can encounter. One example is for the DeleteFile
API:
// Taken from "Part 2" of the BCL Team Blog
using System;
using System.Runtime.InteropServices;
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeleteFile(string lpFileName);
public static void Delete(string fileName)
{
string formattedName = @"\\?\" + fileName;
DeleteFile(formattedName);
}
PI Invoke. But you may not want to use that. uhttp://www.pinvoke.net/default.aspx/kernel32.CreateDirectoryEx
If you use the Unicode version of GetFullPathName it supports up to 32,767 characters.
And same with the CreateFile function that can be used to create directories.
So I'd suggest just finding the suitable functions amount the File Management Functions and then making sure that you're using the Unicode versions.
For example, FindFirstFile could replace Exists
I'd suppose. And combine it with FindNextFile to get the functionality of GetFiles
and GetDirectories
You might not find a replacement for Combine
since I think that that's a .Net specific helper, but on the other hand, you should be able to write your own replacement for that fairly easy.
精彩评论