18

I'm converting a windows forms application to a WPF application. Is there a way to obtain things like, Startup Path, User App Data Path, Common App Data Path, etc. without referencing System.Windows.Forms?

Previously, I used System.Windows.Forms.Application.StartupPath, but the System.Windows.Application.Current object doesn't contain the same information.

dthorpe
  • 34,664
  • 5
  • 74
  • 119
bporter
  • 4,247
  • 2
  • 17
  • 29

4 Answers4

16

You might want to look at System.Environment.GetFolderPath.

The values of the SpecialFolder enum are numerous:

ApplicationData
CommonApplicationData
CommonProgramFiles
Cookies
Desktop
DesktopDirectory
Favorites
History
InternetCache
LocalApplicationData
MyComputer
MyDocuments
MyMusic
MyPictures
Personal
ProgramFiles
Programs
Recent
SendTo
StartMenu
Startup
System
Templates

Is that helpful?

Dan Tao
  • 122,418
  • 53
  • 286
  • 437
6

this will help

for Application.StartupPath use AppDomain.CurrentDomain.BaseDirectory

http://msdn.microsoft.com/en-us/library/system.appdomain.basedirectory.aspx

Camilo Sanchez
  • 1,490
  • 1
  • 14
  • 18
  • 2
    Considering the OP's example was about StartupPath, I believe this should be the accepted answer. SpecialFolder.Startup is something *completely* different. – Riegardt Steyn Apr 25 '13 at 14:32
2

You can use the Environment class to get all types of system information. For directories, use the Environment.GetFolderPath method.

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
mdm20
  • 4,415
  • 2
  • 20
  • 24
  • 1
    Don't use this to store user data, as the Summery states: `The user's profile folder. Applications should not create files or folders at this level; they should put their data under the locations referred to by System.Environment.SpecialFolder.ApplicationData. Added in the .NET Framework 4.` – CularBytes Mar 25 '16 at 13:47
0

I understand and agree that referencing to System.Windows.Forms.dll looks ugly but I think that's better then decrease code re-use and re-invent bicycle instead just one more reference.

I use for example System.Windows.Forms.Application.ProductVersion in my console apps, don't like that but think that's the lesser evil..

See also this question on SO

Community
  • 1
  • 1
abatishchev
  • 95,331
  • 80
  • 293
  • 426