Port to .NET Standard 2.0 - so far works only on .NET Core derived runtimes and .NET Framework, but in the later case the user has to manually copy the dll
This commit is contained in:
parent
4567b0f49c
commit
c11828a7d5
@ -1,22 +1,51 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeFileDialogSharp", "NativeFileDialogSharp\NativeFileDialogSharp.csproj", "{4127F279-9FD5-4C37-B904-242C124C1A07}"
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.0.31919.166
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeFileDialogSharp", "NativeFileDialogSharp\NativeFileDialogSharp.csproj", "{4127F279-9FD5-4C37-B904-242C124C1A07}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeFileDialogSharpSandbox", "NativeFileDialogSharpSandbox\NativeFileDialogSharpSandbox.csproj", "{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeFileDialogSharpSandbox", "NativeFileDialogSharpSandbox\NativeFileDialogSharpSandbox.csproj", "{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeFileDialogSharpSandboxNetFramework", "NativeFileDialogSharpSandboxNetFramework\NativeFileDialogSharpSandboxNetFramework.csproj", "{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
Release|Any CPU = Release|Any CPU
|
Release|Any CPU = Release|Any CPU
|
||||||
|
Release|x64 = Release|x64
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
{4127F279-9FD5-4C37-B904-242C124C1A07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{4127F279-9FD5-4C37-B904-242C124C1A07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{4127F279-9FD5-4C37-B904-242C124C1A07}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{4127F279-9FD5-4C37-B904-242C124C1A07}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{4127F279-9FD5-4C37-B904-242C124C1A07}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{4127F279-9FD5-4C37-B904-242C124C1A07}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
{4127F279-9FD5-4C37-B904-242C124C1A07}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{4127F279-9FD5-4C37-B904-242C124C1A07}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{4127F279-9FD5-4C37-B904-242C124C1A07}.Release|Any CPU.Build.0 = Release|Any CPU
|
{4127F279-9FD5-4C37-B904-242C124C1A07}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{4127F279-9FD5-4C37-B904-242C124C1A07}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{4127F279-9FD5-4C37-B904-242C124C1A07}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Release|Any CPU.Build.0 = Release|Any CPU
|
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Release|x64.Build.0 = Release|x64
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {124CB415-A372-4268-9C13-330A0F1093C2}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
@ -1,56 +1,97 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace NativeFileDialogSharp.Native;
|
namespace NativeFileDialogSharp.Native
|
||||||
|
|
||||||
public struct nfdpathset_t
|
|
||||||
{
|
{
|
||||||
public IntPtr buf;
|
public struct nfdpathset_t
|
||||||
public IntPtr indices;
|
{
|
||||||
public UIntPtr count;
|
public IntPtr buf;
|
||||||
}
|
public IntPtr indices;
|
||||||
|
public UIntPtr count;
|
||||||
public enum nfdresult_t
|
}
|
||||||
{
|
|
||||||
NFD_ERROR,
|
public enum nfdresult_t
|
||||||
NFD_OKAY,
|
{
|
||||||
NFD_CANCEL
|
NFD_ERROR,
|
||||||
}
|
NFD_OKAY,
|
||||||
|
NFD_CANCEL
|
||||||
public static class NativeFunctions
|
}
|
||||||
{
|
|
||||||
public const string LibraryName = "nfd";
|
public static class NativeFunctions
|
||||||
|
{
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
public const string LibraryName = "nfd";
|
||||||
public static extern unsafe nfdresult_t NFD_OpenDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
public static extern unsafe nfdresult_t NFD_OpenDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
|
||||||
public static extern unsafe nfdresult_t NFD_OpenDialogMultiple(byte* filterList, byte* defaultPath, nfdpathset_t* outPaths);
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
public static extern unsafe nfdresult_t NFD_OpenDialogMultiple(byte* filterList, byte* defaultPath,
|
||||||
public static extern unsafe nfdresult_t NFD_SaveDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
|
nfdpathset_t* outPaths);
|
||||||
|
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
public static extern unsafe nfdresult_t NFD_PickFolder(byte* defaultPath, out IntPtr outPath);
|
public static extern unsafe nfdresult_t NFD_SaveDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
|
||||||
|
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
public static extern unsafe byte* NFD_GetError();
|
public static extern unsafe nfdresult_t NFD_PickFolder(byte* defaultPath, out IntPtr outPath);
|
||||||
|
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
public static extern unsafe UIntPtr NFD_PathSet_GetCount(nfdpathset_t* pathSet);
|
public static extern unsafe byte* NFD_GetError();
|
||||||
|
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
public static extern unsafe byte* NFD_PathSet_GetPath(nfdpathset_t* pathSet, UIntPtr index);
|
public static extern unsafe UIntPtr NFD_PathSet_GetCount(nfdpathset_t* pathSet);
|
||||||
|
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
public static extern unsafe void NFD_PathSet_Free(nfdpathset_t* pathSet);
|
public static extern unsafe byte* NFD_PathSet_GetPath(nfdpathset_t* pathSet, UIntPtr index);
|
||||||
|
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
public static extern unsafe void NFD_Dummy();
|
public static extern unsafe void NFD_PathSet_Free(nfdpathset_t* pathSet);
|
||||||
|
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
public static extern unsafe IntPtr NFD_Malloc(UIntPtr bytes);
|
public static extern unsafe void NFD_Dummy();
|
||||||
|
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
public static extern unsafe void NFD_Free(IntPtr ptr);
|
public static extern unsafe IntPtr NFD_Malloc(UIntPtr bytes);
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe void NFD_Free(IntPtr ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class NativeFunctions32
|
||||||
|
{
|
||||||
|
public const string LibraryName = "nfd_x86";
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe nfdresult_t NFD_OpenDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe nfdresult_t NFD_OpenDialogMultiple(byte* filterList, byte* defaultPath,
|
||||||
|
nfdpathset_t* outPaths);
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe nfdresult_t NFD_SaveDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe nfdresult_t NFD_PickFolder(byte* defaultPath, out IntPtr outPath);
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe byte* NFD_GetError();
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe UIntPtr NFD_PathSet_GetCount(nfdpathset_t* pathSet);
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe byte* NFD_PathSet_GetPath(nfdpathset_t* pathSet, UIntPtr index);
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe void NFD_PathSet_Free(nfdpathset_t* pathSet);
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe void NFD_Dummy();
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe IntPtr NFD_Malloc(UIntPtr bytes);
|
||||||
|
|
||||||
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
|
public static extern unsafe void NFD_Free(IntPtr ptr);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,9 +1,8 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<LangVersion>10</LangVersion>
|
<LangVersion>7.3</LangVersion>
|
||||||
<PackageVersion>0.2.5</PackageVersion>
|
<PackageVersion>0.5.0</PackageVersion>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
|
||||||
<Nullable>disable</Nullable>
|
<Nullable>disable</Nullable>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<PackageLicenseExpression>Zlib</PackageLicenseExpression>
|
<PackageLicenseExpression>Zlib</PackageLicenseExpression>
|
||||||
@ -11,6 +10,7 @@
|
|||||||
<PackageProjectUrl>https://github.com/milleniumbug/NativeFileDialogSharp</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/milleniumbug/NativeFileDialogSharp</PackageProjectUrl>
|
||||||
<RepositoryUrl>https://github.com/milleniumbug/NativeFileDialogSharp</RepositoryUrl>
|
<RepositoryUrl>https://github.com/milleniumbug/NativeFileDialogSharp</RepositoryUrl>
|
||||||
<Description>Cross-platform native file dialog controls for Windows, Linux and macOS</Description>
|
<Description>Cross-platform native file dialog controls for Windows, Linux and macOS</Description>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1,156 +1,168 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using NativeFileDialogSharp.Native;
|
using NativeFileDialogSharp.Native;
|
||||||
|
|
||||||
namespace NativeFileDialogSharp;
|
namespace NativeFileDialogSharp
|
||||||
|
|
||||||
public static class Dialog
|
|
||||||
{
|
{
|
||||||
private static byte[] ToUtf8(string s)
|
public static class Dialog
|
||||||
{
|
{
|
||||||
var byteCount = Encoding.UTF8.GetByteCount(s);
|
private static readonly Encoder utf8encoder = Encoding.UTF8.GetEncoder();
|
||||||
var bytes = new byte[byteCount + 1];
|
|
||||||
Encoding.UTF8.GetBytes(s.AsSpan(), bytes.AsSpan());
|
|
||||||
return bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static unsafe Span<byte> MakeSpanFromNullTerminatedString(byte* nullTerminatedString)
|
private static unsafe byte[] ToUtf8(string s)
|
||||||
{
|
|
||||||
int count = 0;
|
|
||||||
var ptr = nullTerminatedString;
|
|
||||||
while (*ptr != 0)
|
|
||||||
{
|
{
|
||||||
ptr++;
|
var byteCount = Encoding.UTF8.GetByteCount(s);
|
||||||
count++;
|
var bytes = new byte[byteCount + 1];
|
||||||
|
fixed (byte* o = bytes)
|
||||||
|
fixed (char* input = s)
|
||||||
|
{
|
||||||
|
utf8encoder.Convert(input, s.Length, o, bytes.Length, true, out _, out var _,
|
||||||
|
out var completed);
|
||||||
|
Debug.Assert(completed);
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Span<byte>(nullTerminatedString, count);
|
private static unsafe int GetNullTerminatedStringLength(byte* nullTerminatedString)
|
||||||
}
|
|
||||||
|
|
||||||
private static string FromUtf8(ReadOnlySpan<byte> input)
|
|
||||||
{
|
|
||||||
return Encoding.UTF8.GetString(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static unsafe DialogResult FileOpen(string filterList = null, string defaultPath = null)
|
|
||||||
{
|
|
||||||
fixed (byte* filterListNts = filterList != null ? ToUtf8(filterList) : null)
|
|
||||||
fixed (byte* defaultPathNts = defaultPath != null ? ToUtf8(defaultPath) : null)
|
|
||||||
{
|
{
|
||||||
string path = null;
|
int count = 0;
|
||||||
string errorMessage = null;
|
var ptr = nullTerminatedString;
|
||||||
var result = NativeFunctions.NFD_OpenDialog(filterListNts, defaultPathNts, out IntPtr outPathIntPtr);
|
while (*ptr != 0)
|
||||||
if (result == nfdresult_t.NFD_ERROR)
|
|
||||||
{
|
{
|
||||||
errorMessage = FromUtf8(MakeSpanFromNullTerminatedString(NativeFunctions.NFD_GetError()));
|
ptr++;
|
||||||
}
|
count++;
|
||||||
else if (result == nfdresult_t.NFD_OKAY)
|
|
||||||
{
|
|
||||||
var outPathNts = (byte*)outPathIntPtr.ToPointer();
|
|
||||||
path = FromUtf8(MakeSpanFromNullTerminatedString(outPathNts));
|
|
||||||
NativeFunctions.NFD_Free(outPathIntPtr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DialogResult(result, path, null, errorMessage);
|
return count;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static unsafe DialogResult FileSave(string filterList = null, string defaultPath = null)
|
private static unsafe string FromUtf8(byte* nullTerminatedString)
|
||||||
{
|
|
||||||
fixed (byte* filterListNts = filterList != null ? ToUtf8(filterList) : null)
|
|
||||||
fixed (byte* defaultPathNts = defaultPath != null ? ToUtf8(defaultPath) : null)
|
|
||||||
{
|
{
|
||||||
string path = null;
|
return Encoding.UTF8.GetString(nullTerminatedString, GetNullTerminatedStringLength(nullTerminatedString));
|
||||||
string errorMessage = null;
|
|
||||||
var result = NativeFunctions.NFD_SaveDialog(filterListNts, defaultPathNts, out IntPtr outPathIntPtr);
|
|
||||||
if (result == nfdresult_t.NFD_ERROR)
|
|
||||||
{
|
|
||||||
errorMessage = FromUtf8(MakeSpanFromNullTerminatedString(NativeFunctions.NFD_GetError()));
|
|
||||||
}
|
|
||||||
else if (result == nfdresult_t.NFD_OKAY)
|
|
||||||
{
|
|
||||||
var outPathNts = (byte*)outPathIntPtr.ToPointer();
|
|
||||||
path = FromUtf8(MakeSpanFromNullTerminatedString(outPathNts));
|
|
||||||
NativeFunctions.NFD_Free(outPathIntPtr);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new DialogResult(result, path, null, errorMessage);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static unsafe DialogResult FolderPicker(string defaultPath = null)
|
public static unsafe DialogResult FileOpen(string filterList = null, string defaultPath = null)
|
||||||
{
|
|
||||||
fixed (byte* defaultPathNts = defaultPath != null ? ToUtf8(defaultPath) : null)
|
|
||||||
{
|
{
|
||||||
string path = null;
|
fixed (byte* filterListNts = filterList != null ? ToUtf8(filterList) : null)
|
||||||
string errorMessage = null;
|
fixed (byte* defaultPathNts = defaultPath != null ? ToUtf8(defaultPath) : null)
|
||||||
var result = NativeFunctions.NFD_PickFolder(defaultPathNts, out IntPtr outPathIntPtr);
|
|
||||||
if (result == nfdresult_t.NFD_ERROR)
|
|
||||||
{
|
{
|
||||||
errorMessage = FromUtf8(MakeSpanFromNullTerminatedString(NativeFunctions.NFD_GetError()));
|
string path = null;
|
||||||
}
|
string errorMessage = null;
|
||||||
else if (result == nfdresult_t.NFD_OKAY)
|
var result = NativeFunctions.NFD_OpenDialog(filterListNts, defaultPathNts, out IntPtr outPathIntPtr);
|
||||||
{
|
if (result == nfdresult_t.NFD_ERROR)
|
||||||
var outPathNts = (byte*)outPathIntPtr.ToPointer();
|
|
||||||
path = FromUtf8(MakeSpanFromNullTerminatedString(outPathNts));
|
|
||||||
NativeFunctions.NFD_Free(outPathIntPtr);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new DialogResult(result, path, null, errorMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static unsafe DialogResult FileOpenMultiple(string filterList = null, string defaultPath = null)
|
|
||||||
{
|
|
||||||
fixed (byte* filterListNts = filterList != null ? ToUtf8(filterList) : null)
|
|
||||||
fixed (byte* defaultPathNts = defaultPath != null ? ToUtf8(defaultPath) : null)
|
|
||||||
{
|
|
||||||
List<string> paths = null;
|
|
||||||
string errorMessage = null;
|
|
||||||
nfdpathset_t pathSet;
|
|
||||||
var result = NativeFunctions.NFD_OpenDialogMultiple(filterListNts, defaultPathNts, &pathSet);
|
|
||||||
if (result == nfdresult_t.NFD_ERROR)
|
|
||||||
{
|
|
||||||
errorMessage = FromUtf8(MakeSpanFromNullTerminatedString(NativeFunctions.NFD_GetError()));
|
|
||||||
}
|
|
||||||
else if (result == nfdresult_t.NFD_OKAY)
|
|
||||||
{
|
|
||||||
var pathCount = (int)NativeFunctions.NFD_PathSet_GetCount(&pathSet).ToUInt32();
|
|
||||||
paths = new List<string>(pathCount);
|
|
||||||
for (int i = 0; i < pathCount; i++)
|
|
||||||
{
|
{
|
||||||
paths.Add(FromUtf8(MakeSpanFromNullTerminatedString(NativeFunctions.NFD_PathSet_GetPath(&pathSet, new UIntPtr((uint)i)))));
|
errorMessage = FromUtf8(NativeFunctions.NFD_GetError());
|
||||||
|
}
|
||||||
|
else if (result == nfdresult_t.NFD_OKAY)
|
||||||
|
{
|
||||||
|
var outPathNts = (byte*)outPathIntPtr.ToPointer();
|
||||||
|
path = FromUtf8(outPathNts);
|
||||||
|
NativeFunctions.NFD_Free(outPathIntPtr);
|
||||||
}
|
}
|
||||||
NativeFunctions.NFD_PathSet_Free(&pathSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new DialogResult(result, null, paths, errorMessage);
|
return new DialogResult(result, path, null, errorMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static unsafe DialogResult FileSave(string filterList = null, string defaultPath = null)
|
||||||
|
{
|
||||||
|
fixed (byte* filterListNts = filterList != null ? ToUtf8(filterList) : null)
|
||||||
|
fixed (byte* defaultPathNts = defaultPath != null ? ToUtf8(defaultPath) : null)
|
||||||
|
{
|
||||||
|
string path = null;
|
||||||
|
string errorMessage = null;
|
||||||
|
var result = NativeFunctions.NFD_SaveDialog(filterListNts, defaultPathNts, out IntPtr outPathIntPtr);
|
||||||
|
if (result == nfdresult_t.NFD_ERROR)
|
||||||
|
{
|
||||||
|
errorMessage = FromUtf8(NativeFunctions.NFD_GetError());
|
||||||
|
}
|
||||||
|
else if (result == nfdresult_t.NFD_OKAY)
|
||||||
|
{
|
||||||
|
var outPathNts = (byte*)outPathIntPtr.ToPointer();
|
||||||
|
path = FromUtf8(outPathNts);
|
||||||
|
NativeFunctions.NFD_Free(outPathIntPtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new DialogResult(result, path, null, errorMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static unsafe DialogResult FolderPicker(string defaultPath = null)
|
||||||
|
{
|
||||||
|
fixed (byte* defaultPathNts = defaultPath != null ? ToUtf8(defaultPath) : null)
|
||||||
|
{
|
||||||
|
string path = null;
|
||||||
|
string errorMessage = null;
|
||||||
|
var result = NativeFunctions.NFD_PickFolder(defaultPathNts, out IntPtr outPathIntPtr);
|
||||||
|
if (result == nfdresult_t.NFD_ERROR)
|
||||||
|
{
|
||||||
|
errorMessage = FromUtf8(NativeFunctions.NFD_GetError());
|
||||||
|
}
|
||||||
|
else if (result == nfdresult_t.NFD_OKAY)
|
||||||
|
{
|
||||||
|
var outPathNts = (byte*)outPathIntPtr.ToPointer();
|
||||||
|
path = FromUtf8(outPathNts);
|
||||||
|
NativeFunctions.NFD_Free(outPathIntPtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new DialogResult(result, path, null, errorMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static unsafe DialogResult FileOpenMultiple(string filterList = null, string defaultPath = null)
|
||||||
|
{
|
||||||
|
fixed (byte* filterListNts = filterList != null ? ToUtf8(filterList) : null)
|
||||||
|
fixed (byte* defaultPathNts = defaultPath != null ? ToUtf8(defaultPath) : null)
|
||||||
|
{
|
||||||
|
List<string> paths = null;
|
||||||
|
string errorMessage = null;
|
||||||
|
nfdpathset_t pathSet;
|
||||||
|
var result = NativeFunctions.NFD_OpenDialogMultiple(filterListNts, defaultPathNts, &pathSet);
|
||||||
|
if (result == nfdresult_t.NFD_ERROR)
|
||||||
|
{
|
||||||
|
errorMessage = FromUtf8(NativeFunctions.NFD_GetError());
|
||||||
|
}
|
||||||
|
else if (result == nfdresult_t.NFD_OKAY)
|
||||||
|
{
|
||||||
|
var pathCount = (int)NativeFunctions.NFD_PathSet_GetCount(&pathSet).ToUInt32();
|
||||||
|
paths = new List<string>(pathCount);
|
||||||
|
for (int i = 0; i < pathCount; i++)
|
||||||
|
{
|
||||||
|
paths.Add(FromUtf8(NativeFunctions.NFD_PathSet_GetPath(&pathSet, new UIntPtr((uint)i))));
|
||||||
|
}
|
||||||
|
|
||||||
|
NativeFunctions.NFD_PathSet_Free(&pathSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new DialogResult(result, null, paths, errorMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DialogResult
|
||||||
|
{
|
||||||
|
private readonly nfdresult_t result;
|
||||||
|
|
||||||
|
public string Path { get; }
|
||||||
|
|
||||||
|
public IReadOnlyList<string> Paths { get; }
|
||||||
|
|
||||||
|
public bool IsError => result == nfdresult_t.NFD_ERROR;
|
||||||
|
|
||||||
|
public string ErrorMessage { get; }
|
||||||
|
|
||||||
|
public bool IsCancelled => result == nfdresult_t.NFD_CANCEL;
|
||||||
|
|
||||||
|
public bool IsOk => result == nfdresult_t.NFD_OKAY;
|
||||||
|
|
||||||
|
internal DialogResult(nfdresult_t result, string path, IReadOnlyList<string> paths, string errorMessage)
|
||||||
|
{
|
||||||
|
this.result = result;
|
||||||
|
Path = path;
|
||||||
|
Paths = paths;
|
||||||
|
ErrorMessage = errorMessage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DialogResult
|
|
||||||
{
|
|
||||||
private readonly nfdresult_t result;
|
|
||||||
|
|
||||||
public string Path { get; }
|
|
||||||
|
|
||||||
public IReadOnlyList<string> Paths { get; }
|
|
||||||
|
|
||||||
public bool IsError => result == nfdresult_t.NFD_ERROR;
|
|
||||||
|
|
||||||
public string ErrorMessage { get; }
|
|
||||||
|
|
||||||
public bool IsCancelled => result == nfdresult_t.NFD_CANCEL;
|
|
||||||
|
|
||||||
public bool IsOk => result == nfdresult_t.NFD_OKAY;
|
|
||||||
|
|
||||||
internal DialogResult(nfdresult_t result, string path, IReadOnlyList<string> paths, string errorMessage)
|
|
||||||
{
|
|
||||||
this.result = result;
|
|
||||||
Path = path;
|
|
||||||
Paths = paths;
|
|
||||||
ErrorMessage = errorMessage;
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,7 +8,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="NativeFileDialogSharp" Version="0.2.4" />
|
<PackageReference Include="NativeFileDialogSharp" Version="0.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -1,14 +1,26 @@
|
|||||||
// See https://aka.ms/new-console-template for more information
|
using NativeFileDialogSharp;
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using NativeFileDialogSharp;
|
|
||||||
using NativeFileDialogSharp.Native;
|
|
||||||
|
|
||||||
var result = Dialog.FileOpenMultiple();
|
namespace NativeFileDialogSharpSandbox
|
||||||
|
|
||||||
Console.WriteLine($"Path: {result.Path}, IsError {result.IsError}, IsOk {result.IsOk}, IsCancelled {result.IsCancelled}, ErrorMessage {result.ErrorMessage}");
|
|
||||||
if (result.Paths != null)
|
|
||||||
{
|
{
|
||||||
Console.WriteLine("Paths");
|
internal class Program
|
||||||
Console.WriteLine(string.Join("\n", result.Paths));
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
PrintResult(Dialog.FileOpenMultiple("pdf", null));
|
||||||
|
PrintResult(Dialog.FileOpen(null));
|
||||||
|
PrintResult(Dialog.FileSave(null));
|
||||||
|
PrintResult(Dialog.FolderPicker(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void PrintResult(DialogResult result)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Path: {result.Path}, IsError {result.IsError}, IsOk {result.IsOk}, IsCancelled {result.IsCancelled}, ErrorMessage {result.ErrorMessage}");
|
||||||
|
if (result.Paths != null)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Paths");
|
||||||
|
Console.WriteLine(string.Join("\n", result.Paths));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
6
NativeFileDialogSharpSandboxNetFramework/App.config
Normal file
6
NativeFileDialogSharpSandboxNetFramework/App.config
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<configuration>
|
||||||
|
<startup>
|
||||||
|
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
|
||||||
|
</startup>
|
||||||
|
</configuration>
|
@ -0,0 +1,78 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProjectGuid>{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}</ProjectGuid>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<RootNamespace>NativeFileDialogSharpSandboxNetFramework</RootNamespace>
|
||||||
|
<AssemblyName>NativeFileDialogSharpSandboxNetFramework</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||||
|
<Deterministic>true</Deterministic>
|
||||||
|
<TargetFrameworkProfile />
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<PlatformTarget>x86</PlatformTarget>
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<OutputPath>bin\x64\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<PlatformTarget>x64</PlatformTarget>
|
||||||
|
<LangVersion>7.3</LangVersion>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<Prefer32Bit>true</Prefer32Bit>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||||
|
<OutputPath>bin\x64\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<PlatformTarget>x64</PlatformTarget>
|
||||||
|
<LangVersion>7.3</LangVersion>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<Prefer32Bit>true</Prefer32Bit>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="NativeFileDialogSharp, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\NativeFileDialogSharp.0.4.4\lib\net461\NativeFileDialogSharp.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
<Reference Include="Microsoft.CSharp" />
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Net.Http" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="Program.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="App.config" />
|
||||||
|
<None Include="packages.config" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
</Project>
|
26
NativeFileDialogSharpSandboxNetFramework/Program.cs
Normal file
26
NativeFileDialogSharpSandboxNetFramework/Program.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using NativeFileDialogSharp;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace NativeFileDialogSharpSandboxNetFramework
|
||||||
|
{
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
PrintResult(Dialog.FileOpenMultiple("pdf", null));
|
||||||
|
PrintResult(Dialog.FileOpen(null));
|
||||||
|
PrintResult(Dialog.FileSave(null));
|
||||||
|
PrintResult(Dialog.FolderPicker(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void PrintResult(DialogResult result)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Path: {result.Path}, IsError {result.IsError}, IsOk {result.IsOk}, IsCancelled {result.IsCancelled}, ErrorMessage {result.ErrorMessage}");
|
||||||
|
if (result.Paths != null)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Paths");
|
||||||
|
Console.WriteLine(string.Join("\n", result.Paths));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// General Information about an assembly is controlled through the following
|
||||||
|
// set of attributes. Change these attribute values to modify the information
|
||||||
|
// associated with an assembly.
|
||||||
|
[assembly: AssemblyTitle("NativeFileDialogSharpSandboxNetFramework")]
|
||||||
|
[assembly: AssemblyDescription("")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("")]
|
||||||
|
[assembly: AssemblyProduct("NativeFileDialogSharpSandboxNetFramework")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright © 2021")]
|
||||||
|
[assembly: AssemblyTrademark("")]
|
||||||
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
// Setting ComVisible to false makes the types in this assembly not visible
|
||||||
|
// to COM components. If you need to access a type in this assembly from
|
||||||
|
// COM, set the ComVisible attribute to true on that type.
|
||||||
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
|
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||||
|
[assembly: Guid("0fd91168-d8f5-4776-8d91-cb9b9c55aa1b")]
|
||||||
|
|
||||||
|
// Version information for an assembly consists of the following four values:
|
||||||
|
//
|
||||||
|
// Major Version
|
||||||
|
// Minor Version
|
||||||
|
// Build Number
|
||||||
|
// Revision
|
||||||
|
//
|
||||||
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
|
// by using the '*' as shown below:
|
||||||
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
|
[assembly: AssemblyVersion("1.0.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
5
NativeFileDialogSharpSandboxNetFramework/packages.config
Normal file
5
NativeFileDialogSharpSandboxNetFramework/packages.config
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<packages>
|
||||||
|
<package id="Microsoft.NETCore.Platforms" version="6.0.1" targetFramework="net462" />
|
||||||
|
<package id="NativeFileDialogSharp" version="0.4.4" targetFramework="net462" />
|
||||||
|
</packages>
|
Loading…
Reference in New Issue
Block a user