ja13 ja13 - 1 month ago 20
C# Question

Visual Studio 2015 designer crash

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?

Answer

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