I have a form which is derived from an existing form. The existing form works perfectly. When I attempt to open the new form (for the first time) I get an error from the designer:
To prevent possible data loss before loading the designer, the following errors must be resolved:
Access to the path 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Logs\eventlog.dat' is denied.
This file does not even exist. If I look at the call stack it gets even more strange. In the constructor of the form being derived from, there is a class that creates a log file.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at MeterView.MVLogger..ctor() in C:\Users\glevenson\Documents\Visual Studio 2010\Projects\MeterView\MeterView\MVDataLogger.cs:line 67
at MeterView.MVLogger.get_Instance() in C:\Users\glevenson\Documents\Visual Studio 2010\Projects\MeterView\MeterView\MVDataLogger.cs:line 79
at MeterView.frmSingle..ctor() in C:\Users\glevenson\Documents\Visual Studio 2010\Projects\MeterView\MeterView\frmSingle.cs:line 167
Which is referencing a line code that does in fact create a directory and log file, but from the applications current directory. This makes no sense at all.
Why is a warning being generated about a file that doesn't exist, and why is it preventing me from doing anything in the designer? At the moment I am completely dead in the water because of this.
Thanks for any advice.
Two mistakes you are making here. The first one is that you don't specify the full path name of the file. You are creating/opening the logging file by just specifying "eventlog.dat". That goes wrong when the default working directory is not set where you hope it is. It certainly isn't set anywhere happy in design mode. It is never set correctly after you ship your project, you cannot write to a subdirectory of c:\program files. You must use Environment.GetFolderPath() to select an AppData path that you can write to.
Second mistake is that you allow this code to run in design mode. Use the DesignMode property to prevent that from happening.