LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

[C#] 動かすアプリのプロセスがx64かx86かによって、SpecialFolderのパスが変わる

Last updated at Posted at 2020-09-03

やりたいこと

コード上で、Program Filesのフォルダを取ってくるときに、
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)を使ってパスを取得していたが、ある日「なんかいつの間にかフォルダのパス変わってるんですけど?」と言われてしまった。
(この時は、ファイル選択Dlgの表示時の初期パスとして、上記を使っていた)

なぜそうなったか?調べたい。

なぜそうなったか?

以前は対象のアプリをAnyCPUでビルドしていたが、今はx64でビルドすることになっていた。
使っていたEnvironment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)は、ビルド時のプラットフォームを何にするか(=x64,86どちらのプロセスとして動作するか)で返すパスが変わる。それが原因だった。

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)の返す値

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)と
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)の返すパスは
ビルド時のプラットフォーム設定によって下記の通り。

image.png
※32bitWindowsで動かしたときにどうなるかは試してない。

参考

MSDocs(Environment.SpecialFolder Enum)
https://docs.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=netcore-3.1
ここに書かれていた。
image.png

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up