Rohit
Rohit

Reputation: 7629

What is the difference between Environment.CurrentDirectory and Directory.GetCurrentDirectory?

In .NET what is the difference between:

Of course, Environment.CurrentDirectory is a property which can be set and obtained.

Are there any other differences?

Upvotes: 41

Views: 14035

Answers (5)

AakashM
AakashM

Reputation: 63340

As per other answers, there is no difference - the implemenetation of Environment.CurrentDirectory delegates to the Get and Set methods in Directory.

There's an interesting stylistic API-design question that raises - why did the designers of Environment feel that a regular property was appropriate, whereas the designers of Directory preferred explicit Get and Set methods?

The Framework Design Guidelines book has a fair amount to say about choosing properties versus methods, some of which is available online. The most relevant parts seem to me to be (with my emphases):

A rule of thumb is that methods should represent actions and properties should represent data. Properties are preferred over methods if everything else is equal

...

  • CONSIDER using a property, if the member represents a logical attribute of the type

...

  • DO use a method, rather than a property, in the following situations:
    • The operation is orders of magnitude slower than a field access would be

All things considered my opinion is that explicit Get and Set methods better represent what is going on here.

Upvotes: 31

mdjdot
mdjdot

Reputation: 1

public static string GetCurrentDirectory (); https://referencesource.microsoft.com/#mscorlib/system/io/directory.cs,4de7d0d0c291277b

public static string CurrentDirectory { get; set; } https://referencesource.microsoft.com/#mscorlib/system/environment.cs,b64586ead0df012b

Although the Environment.CurrentDirectory called Directory.GetCurrentDirectory, I still prefer to use Environment.CurrentDirectory, since it's a static property, can response faster.

Upvotes: -2

Chris Webb
Chris Webb

Reputation: 11

Directory.SetCurrentDirectory throws no fewer than 7 exceptions, so checking up on all the things that could go wrong could be time-consuming. The method therefore presumably complies with

"DO use a method, rather than a property, in the following situations:

The operation is orders of magnitude slower than a field access would be"

Even GetCurrentDirectory has 2 potential exceptions. Across a network and/or with a large number of subdirectories to navigate these could potentially take seconds instead of milliseconds in the worst case.

Upvotes: 1

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

As David says: they do the same thing. Internally, when getting Environment.CurrentDirectory it will call Directory.GetCurrentDirectory and when setting Environment.CurrentDirectory it will call Directory.SetCurrentDirectory.

Just pick a favorite and go with it.

Upvotes: 46

David M
David M

Reputation: 72840

No, there are no other differences.

Upvotes: 2

Related Questions