Open BanjoBenStanford opened 2 years ago
make sure you have normal access to UNC files, even without litedb. try
Console.WriteLine(File.Exists(LDBPath));
then if not Access, you can try
public class ConnectToSharedFolder : IDisposable
{
readonly string _networkName;
public ConnectToSharedFolder(string networkName, NetworkCredential credentials)
{
_networkName = networkName;
var netResource = new NetResource
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};
var userName = string.IsNullOrEmpty(credentials.Domain)
? credentials.UserName
: string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);
var result = WNetAddConnection2(
netResource,
credentials.Password,
userName,
0);
if (result != 0)
{
throw new Win32Exception(result, "Error connecting to remote share");
}
}
~ConnectToSharedFolder()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
WNetCancelConnection2(_networkName, 0, true);
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags,
bool force);
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
}
public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}
}
public string networkPath = @"\\{Your IP or Folder Name of Network}\Shared Data";
NetworkCredential credentials = new NetworkCredential(@"{User Name}", "{Password}");
using (new ConnectToSharedFolder(networkPath, credentials))
{
Console.WriteLine(File.Exists(LDBPath));
//if true
//then try connect litedb
}
I'm getting this as well. It takes too long for the file to release, so on quick repeat access, you get the error. If its one-off, or for an extended time before repeat access, its fine.
All other file operations on the UNC path is fine for all other services except litedb.
Version LiteDB 5.0.12
Describe the bug When using a UNC path to a network share location the Using statement sometimes fails to dispose the file stream. It works fine with a path to a local file.
Code to Reproduce App.Config
Program.cs
Expected behavior Expect the function to operate without error when using UNC network file share paths. Local paths work fine.
Screenshots/Stacktrace
System.UnauthorizedAccessException HResult=0x80070005 Message=Access to the path is denied. Source=mscorlib StackTrace: at System.IO.Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Error.WinIOError() at System.IO.FileStream.FlushOSBuffer() at System.IO.FileStream.Flush(Boolean flushToDisk) at LiteDB.StreamExtensions.FlushToDisk(Stream stream) at LiteDB.Engine.DiskService.Write(IEnumerable`1 pages, FileOrigin origin) at LiteDB.Engine.WalIndexService.CheckpointInternal() at LiteDB.Engine.WalIndexService.TryCheckpoint() at LiteDB.Engine.LiteEngine.Dispose(Boolean disposing) at LiteDB.Engine.LiteEngine.Dispose() at LiteDB.LiteDatabase.Dispose(Boolean disposing) at LiteDB.LiteDatabase.Dispose() at TSIA2_Scores_Import.Form1.GetTestCounts(String LDBPath) in Q:\System Support Team\C# Programs\Test_Scores\Test_Scores\TSIA2 Scores Import\Form1.cs:line 5496 at TSIA2_Scores_Import.Form1.bGotoScoreCount_Click(Object sender, EventArgs e) in Q:\System Support Team\C# Programs\Test_Scores\Test_Scores\TSIA2 Scores Import\Form1.cs:line 5555 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at TSIA2_Scores_Import.Program.Main() in Q:\System Support Team\C# Programs\Test_Scores\Test_Scores\TSIA2 Scores Import\Program.cs:line 21
Additional context Add any other context about the problem here.