In relation my other question, I've tried to write a small program that sets a files short name given it's long name. However I'm always getting an exception thrown:
Error: The parameter is incorrect
on FAT32
Error: The handle is invalid
on NTFS
Edit: I now appreciate that I cannot call the API under FAT32
FYI: I'm running under an Administrative Console.
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.ComponentModel;
namespace Test{
public static class Program
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
public extern static IntPtr CreateFile(
string lpFileName, uint dwDesiredAccess, uint dwShareMode,
IntPtr SecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile
);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetFileShortName(IntPtr hFile, string lpShortName);
public static void Main(string[] args)
{
const uint GENERIC_ALL = 0x10000000;
const uint FILE_SHARE_READ = 0x00000001;
const uint FILE_SHARE_WRITE = 0x00000002;
const uint FILE_SHARE_DELETE = 0x00000003;
const uint OPEN_EXISTING = 3;
const uint BACKUP_SEMANTICS = 0x02000000;
var handle = CreateFile(args[0], GENERIC_ALL, FILE_SHARE_READ,
IntPtr.Zero, OPEN_EXISTING, BACKUP_SEMANTICS, IntPtr.Zero);
bool ret = SetFileShortName(handle, args[1]);
if (!ret)
{
Console.WriteLine("Error: {0}", new Win32Exception(Marshal.GetLastWin32Error()).Message);
}
}
}
}
It's the same if I use FILE_SHARE DELETE or WRITE