How to resolve this System.IO.FileNotFoundException


How to resolve this System.IO.FileNotFoundException



Event Viewer


Application: The.Application.Name.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
Stack:

at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(
System.Object, System.Delegate, System.Object, Int32, System.Delegate)

at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(
System.Windows.Threading.DispatcherPriority, System.TimeSpan,
System.Delegate, System.Object, Int32)

at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)

at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)

at System.Windows.Threading.Dispatcher.PushFrameImpl(
System.Windows.Threading.DispatcherFrame)

at System.Windows.Threading.Dispatcher.PushFrame(
System.Windows.Threading.DispatcherFrame)

at System.Windows.Threading.Dispatcher.Run()

at System.Windows.Application.RunDispatcher(System.Object)

at System.Windows.Application.RunInternal(System.Windows.Window)

at System.Windows.Application.Run(System.Windows.Window)

at The.Application.Name.Main()



BtnUseDesktop_Click


private void BtnUseDesktop_Click(object sender, RoutedEventArgs e)
{
AvSwitcher switcher = new AvSwitcher();
this.RunAsyncTask(() =>
switcher.SwitchToDesktop(this.windowSyncSvc.ActiveLyncWindowHandle));
}



The AvSwitcher that the Click Event Calls Into


public class AvSwitcher
{
private DeviceLocationSvc deviceLocationSvc;
private UIAutomationSvc uiAutomationSvc;
private WindowMovingSvc windowMovingSvc;
private ManualResetEvent manualResetEvent;
private Modality audioVideo;
public static bool IsSwitching { get; set; }

public AvSwitcher()
{
this.deviceLocationSvc = new DeviceLocationSvc();
this.uiAutomationSvc = new UIAutomationSvc();
this.windowMovingSvc = new WindowMovingSvc();
}

public void SwitchToDesktop(IntPtr activeLyncConvWindowHandle)
{
this.BeginHold(DeviceLocation.Desktop, activeLyncConvWindowHandle);
}

public void SwitchToWall(IntPtr activeLyncConvWindowHandle)
{
this.BeginHold(DeviceLocation.Wall, activeLyncConvWindowHandle);
}

private Conversation GetLyncConversation()
{
Conversation conv = null;
if (LyncClient.GetClient() != null)
{
conv = LyncClient.GetClient().ConversationManager.Conversations.FirstOrDefault();
}

return conv;
}

private void BeginHold(DeviceLocation targetLocation, IntPtr activeLyncConvWindowHandle)
{
AvSwitcher.IsSwitching = true;

// make sure the class doesn't dispose of itself
this.manualResetEvent = new ManualResetEvent(false);

Conversation conv = this.GetLyncConversation();
if (conv != null)
{
this.audioVideo = conv.Modalities[ModalityTypes.AudioVideo];
ModalityState modalityState = this.audioVideo.State;

if (modalityState == ModalityState.Connected)
{
this.HoldCallAndThenDoTheSwitching(targetLocation, activeLyncConvWindowHandle);
}
else
{
this.DoTheSwitching(targetLocation, activeLyncConvWindowHandle);
}
}
}

private void HoldCallAndThenDoTheSwitching(
DeviceLocation targetLocation,
IntPtr activeLyncConvWindowHandle)
{
try
{
this.audioVideo.BeginHold(
this.BeginHold_callback,
new AsyncStateValues()
{
TargetLocation = targetLocation,
ActiveLyncConvWindowHandle = activeLyncConvWindowHandle
});
this.manualResetEvent.WaitOne();
}
catch (UnauthorizedAccessException)
{
// the call is already on hold
this.DoTheSwitching(targetLocation, activeLyncConvWindowHandle);
}
}

private void BeginHold_callback(IAsyncResult ar)
{
if (ar.IsCompleted)
{
DeviceLocation targetLocation = ((AsyncStateValues)ar.AsyncState).TargetLocation;
IntPtr activeLyncConvWindowHandle =
((AsyncStateValues)ar.AsyncState).ActiveLyncConvWindowHandle;
this.DoTheSwitching(targetLocation, activeLyncConvWindowHandle);
}

Thread.Sleep(2000); // is this necessary
this.audioVideo.BeginRetrieve(this.BeginRetrieve_callback, null);
}

private void DoTheSwitching(DeviceLocation targetLocation, IntPtr activeLyncConvWindowHandle)
{
DeviceLocationSvc.TargetDevices targetDevices =
this.deviceLocationSvc.GetTargetDevices(targetLocation);

this.SwitchScreenUsingWinApi(targetDevices.Screen, activeLyncConvWindowHandle);
this.SwitchVideoUsingLyncApi(targetDevices.VideoDevice);
this.SwitchAudioUsingUIAutomation(
targetDevices.MicName,
targetDevices.SpeakersName,
activeLyncConvWindowHandle);

AvSwitcher.IsSwitching = false;
}

private void SwitchScreenUsingWinApi(Screen targetScreen, IntPtr activeLyncConvWindowHandle)
{
if (activeLyncConvWindowHandle != IntPtr.Zero)
{
WindowPosition wp =
this.windowMovingSvc.GetTargetWindowPositionFromScreen(targetScreen);
this.windowMovingSvc.MoveTheWindowToTargetPosition(activeLyncConvWindowHandle, wp);
}
}

private void SwitchVideoUsingLyncApi(VideoDevice targetVideoDevice)
{
if (targetVideoDevice != null)
{
LyncClient.GetClient().DeviceManager.ActiveVideoDevice = targetVideoDevice;
}
}

private void SwitchAudioUsingUIAutomation(
string targetMicName,
string targetSpeakersName,
IntPtr activeLyncConvWindowHandle)
{
if (targetMicName != null && targetSpeakersName != null)
{
AutomationElement lyncConvWindow =
AutomationElement.FromHandle(activeLyncConvWindowHandle);

AutomationElement lyncOptionsWindow =
this.uiAutomationSvc.OpenTheLyncOptionsWindowFromTheConvWindow(lyncConvWindow);

this.uiAutomationSvc.SelectTheTargetMic(lyncOptionsWindow, targetMicName);

this.uiAutomationSvc.SelectTheTargetSpeakers(lyncOptionsWindow, targetSpeakersName);

this.uiAutomationSvc.InvokeOkayButton(lyncOptionsWindow);
}
}

private void BeginRetrieve_callback(IAsyncResult ar)
{
this.audioVideo.EndRetrieve(ar);
this.manualResetEvent.Set(); // allow the program to exit
}

private class AsyncStateValues
{
internal DeviceLocation TargetLocation { get; set; }

internal IntPtr ActiveLyncConvWindowHandle { get; set; }
}
}





Can you post corresponding code?
– Szymon
Mar 31 '14 at 23:31





Looks like the file wasn't found. Without code it is impossible to determine why.
– Erik Philips
Mar 31 '14 at 23:31





"Main" is the entry point for any C# program, try turning "FileNotFoundException" on in your debug exception settings to see the actual line. Some code would be nice as well.
– BradleyDotNET
Mar 31 '14 at 23:34





@ShaunLuttin I have NO idea, there is code code nor is there any verbose logging of the exception provided.
– Erik Philips
Mar 31 '14 at 23:35





@ErikPhilips There is always a main method in a WPF program, whether you personally write it or not. See this question.
– mason
Apr 1 '14 at 16:07





4 Answers
4



I hate to point out the obvious, but System.IO.FileNotFoundException means the program did not find the file you specified. So what you need to do is check what file your code is looking for in production.



To see what file your program is looking for in production (look at the FileName property of the exception), try these techniques:



Then look at the file system on the machine and see if the file exists. Most likely the case is that it doesn't exist.





I marked this as the answer because it lead me to use "attach to process." Then I edited the answer to include attach to process as a production debug option.
– Shaun Luttin
Apr 1 '14 at 16:14






What do you mean by "write to a debug log" and what to write?
– usefulBee
Feb 20 at 21:48





@usefulBee It means that you need to store some record of what happened and the relevant information that would be useful when debugging. You can roll your own logging solution, but it's highly recommended to take a look at pre-existing libraries such as Serilog, NLog etc.
– mason
Feb 20 at 21:51





Does this mean it is not possible to find out the missing file from the data provided by Event Viewer?
– usefulBee
Feb 20 at 22:05





@usefulBee It depends on what’s in the event viewer. I certainly wouldn’t rely on what’s in it. Take logging into your own hands so you control exactly what gets logged.
– mason
Feb 20 at 22:31




I came across a similar situation after publishing a ClickOnce application, and one of my colleagues on a different domain reported that it fails to launch.



To find out what was going on, I added a try catch statement inside the MainWindow method as @BradleyDotNET mentioned in one comment on the original post, and then published again.


public MainWindow()
{
try
{
InitializeComponent();
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}



Then my colleague reported to me the exception detail, and it was a missing reference of a third party framework dll file.



Added the reference and problem solved.



I've been mislead by this error more than once. After spending hours googling, updating nuget packages, version checking, then after sitting with a completely updated solution I re-realize a perfectly valid, simpler reason for the error.



If in a threaded enthronement (UI Dispatcher.Invoke for example), System.IO.FileNotFoundException is thrown if the thread manager dll (file) fails to return. So if your main UI thread A, calls the system thread manager dll B, and B calls your thread code C, but C throws for some unrelated reason (such as null Reference as in my case), then C does not return, B does not return, and A only blames B with FileNotFoundException for being lost...



Before going down the dll version path... Check closer to home and verify your thread code is not throwing.



Check all the references carefully



For me cleaning entire solution by deleting manually, updating (removing and adding) references again with version in sync with target machine and then building with with Copy Local > False for GAC assemblies solves the problem.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV