0

I have this method:

static public void WriteErrorToFile(string ErrorMassege, string HelpLink)
{
    string text = DateTime.Now.ToString() + ":" + Environment.NewLine + ErrorMassege + Environment.NewLine;

    if (!string.IsNullOrWhiteSpace(HelpLink))
    {
        text += "Help link: " + HelpLink + Environment.NewLine;
    }

    text += Environment.NewLine;
    File.AppendAllText("errors.txt", text);
}

I call this method every time some expception occurred, but then the designer in Visual Studio 2015 crashes with massege:

UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\errors.txt' is denied.

If that mean something to you I call this function in MyViewModel what is datacontext of my page. If I delete errors.txt file then everything is normal again. Anyone knows why this happen and what is the soulution?

Guilherme Fidelis
  • 1,022
  • 11
  • 20
ja13
  • 89
  • 1
  • 8
  • 1
    The easier is probably to distinct [design time](http://stackoverflow.com/q/834283/1997232) and don't do anything what may crash designer. Also, during debugging you shouldn't handle everything, check for [debugger presence](https://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.isattached(v=vs.110).aspx) and e.g. don't handle unhandled exceptions in this case. – Sinatr May 25 '16 at 10:25

1 Answers1

2

the problem is that the WPF designer has to execute your code to see what data is available to it on the viewModel the DesignerProperties contains a method GetIsInDesignMode that you can use to disable logic when your code is being executed by the designer

MikeT
  • 5,398
  • 3
  • 27
  • 43