There is a problem when displaying UserCotrol in the designer, there is the following code in the UserControl designer

var dir = new DirectoryInfo(@"AutoScenario"); LstScenario.Items.Clear(); foreach (FileInfo subDir in dir.GetFiles()) { LstScenario.Items.Add(subDir.Name); } 

the program works without problems but does not display UserControl in the designer while writing an error referring to the wrong path

 ошибка: Could not find a part of the path 'C:\Windows\system32\AutoScenario'. 
  • And you should not access the file system in your UI, otherwise it will be understandable that it will hang and fail. The file list should come from the VM. - VladD
  • The question of why during the execution of the program and to represent the designer in different ways? - XmaksasX
  • Why should they be the same? How does the designer know what the working directory of your program will be? You know that the working directory and the directory in which the program is located are unrelated things? - VladD
  • I agree that the designer does not know what the working directory will be, so he defaults to "C: \ Windows \ system32 \"? and after starting it gets the working directory so it understands the relative path? how, then, specify the path relative to the exe file - XmaksasX
  • Well, here is a whole discussion of this problem: stackoverflow.com/a/6041505/276994 - VladD

1 answer 1

You can execute different branches of the code depending on whether the code is executed in the designer or in runtime.

 if (DesignerProperties.GetIsInDesignMode(this)) { // В дизайнере } else { // В рантайме } 

You can set the working directory for debugging in the project properties. Or in runtime using the Directory.SetCurrentDirectory method

But you are rightly advised that synchronous operations with the file system will sooner or later suspend the UI, which will generate a lot of negativity from the users.

  • Sorry for the stupid question, but what does "synchronous operations with the file system" mean? If you find it difficult to answer, tell in which direction to read about it? - XmaksasX
  • @XmaksasX in this case means a synchronous call waiting for the completion of the operation. Suppose the working directory is located on the network ball. You send a request using the SMB protocol, and the remote machine for some reason does not respond or responds with a delay. As a result, the UI hangs up for 15 seconds, and the system offers to crash the non-responding application. The user is wondering if some work is happening now or not. And you can read about await / async in the realities of WPF and async binding. - Lunar Whisper
  • Many thanks went to study. - XmaksasX