C#: Maximum file name length for OLE file
I used WinAPI function to create OLE file in C#. The WinAPI function is:
[DllImport("ole32.dll")]
public static extern int OleCreateFromFile(
[In] ref Guid rclsid,
[MarshalAs(UnmanagedType.LPWStr)] string lpszFileName,
[In] ref Guid riid, uint renderopt,
[In] IntPtr pFormatEtc,
IOleClientSite pClientSite,
IStorage pStg,
[MarshalAs(UnmanagedType.IUnknown)] out object ppvObj);
I used this function in C# application as below:
hResult = OleAPI.OleCreateFromFile(ref clsid,
sourceFileFullPath,
ref IID_IOleObject,
(uint)OLERENDER.OLERENDER_DRAW,
IntPtr.Zero,
pOleClientSite,
pStorage,
out pOleObjectOut);
The function works perfectly. But when the length of filename of the file is 140, it failed to create OLE file. The return value of hResult is -2147467259.
The value of sourceFileFullPath="C:\Users\Administrator\AppData\Local\Temp\TmpTzStore1\pju3wswj.jmq\000008F6\CalendarRepeddsfsfdsfsfat344Issue5sdfdfsfsfdsfdsfsdsdfdsfsf00kbCalendarRepeatIssue500kbCalendarRepeatIssue500kbCa开发者_JAVA百科lendarRepeatIssue500kb.xlsx"
The length of filename (CalendarRepeddsfsfdsfsfat344Issue5sdfdfsfsfdsfdsfsdsdfdsfsf00kbCalendarRepeatIssue500kbCalendarRepeatIssue500kbCalendarRepeatIssue500kb.xlsx) is 140.
But when I trimmed the file name to 120, it worked.
I wanted to know if there is any limitation in the filename length for OLE file. Since I tried it for different application files( doc,docx,txt,pdf...). The results varied for different files.
I don't know what is the exact the file name length that this API supports. Or it is dependent on the OS? I am using Windows Server 2008.
Thanks Prakash
The maximum file name length of the complete filename length, including the path, is (about) 259 characters. The length of the path you pasted is 216 characters, so this should be under the limit.
That the MSDN pages do not describe a specific maximum length implies that 259 characters should be the maximum.
I could not find any documentation on this discrepancy. Maybe one or more of your paths are softlinks to path names that are in reality longer.
精彩评论