Updated NormFile.Name to use stackalloc (#37)
parent
bc8ab6b8d0
commit
f04faca08a
|
|
@ -25,27 +25,18 @@ namespace Mil.Navy.Nrl.Norm
|
||||||
/// The name of the file.
|
/// The name of the file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <exception cref="IOException">Thrown when failed to get file name.</exception>
|
/// <exception cref="IOException">Thrown when failed to get file name.</exception>
|
||||||
public string Name
|
public unsafe string Name
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
var buffer = new byte[FILENAME_MAX];
|
var buffer = stackalloc byte[FILENAME_MAX];
|
||||||
var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
|
var bufferPtr = (nint)buffer;
|
||||||
string? name;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var bufferPtr = bufferHandle.AddrOfPinnedObject();
|
|
||||||
if (!NormFileGetName(_handle, bufferPtr, FILENAME_MAX))
|
if (!NormFileGetName(_handle, bufferPtr, FILENAME_MAX))
|
||||||
{
|
{
|
||||||
throw new IOException("Failed to get file name");
|
throw new IOException("Failed to get file name");
|
||||||
}
|
}
|
||||||
name = Marshal.PtrToStringAnsi(bufferPtr);
|
var name = Marshal.PtrToStringAnsi(bufferPtr);
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
bufferHandle.Free();
|
|
||||||
}
|
|
||||||
|
|
||||||
return name ?? string.Empty;
|
return name ?? string.Empty;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue