Среда. CurrentDirectory приводит к неожиданным результатам когда запускающее установленное приложение

Как упомянуто размер должен отразить текущую архитектуру. Вы могли взять пик вокруг в limits.h, если Вы хотите видеть, как Ваш текущий компилятор обрабатывает вещи.

15
задан devuxer 27 August 2009 в 21:23
поделиться

5 ответов

Если вы хотите получить путь к каталогу, в котором работает ваш исполняемый файл, вам не следует полагаться на Environment.CurrentDirectory, поскольку его можно изменить несколькими способами (настройки shotrtcut, так далее). Вместо этого попробуйте один из следующих вариантов:

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

или

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
35
ответ дан 1 December 2019 в 00:23
поделиться

Поскольку вы сказали, что ваше приложение использует WPF, вы можете использовать приведенный ниже код вместо Application.StartupPath:

String appPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
3
ответ дан 1 December 2019 в 00:23
поделиться

Use Application.StartupPath instead of Environment.CurrentDirectory.
I've had a similar problem, where the CurrentDirectory was being changed inadvertently by something like an OpenFileDialog without me even realizing it.
In your case, it sounds like the process that you're starting the application form is changing the CurrentDirectory unbeknownst to you.

8
ответ дан 1 December 2019 в 00:23
поделиться

When a program is started, the current directory is typically the same as the one of the starting application, unless the starting application specifies a different working directory. It can really be anywhere on disk.

In your case, the starting application is the shell (explorer.exe) in both cases. It does specify a working directory when starting a program, depending on the context of the launch. You have seen two different cases (double-clicking on a file in explorer, and running from the start menu); you have also found what Microsoft considers the most sensible values for current directory in either case: the user's home directory, and the directory that is shown in explorer (respectively).

1
ответ дан 1 December 2019 в 00:23
поделиться

Environment.CurrentDirectory содержит текущий каталог, который на самом деле является текущим каталогом. Стоимость зависит от многих факторов. Любое приложение может изменить значение. Это значение не относится только к вашему приложению.

Если вы хотите получить каталог запуска, используйте Application.StartupPath .

2
ответ дан 1 December 2019 в 00:23
поделиться
Другие вопросы по тегам:

Похожие вопросы: