The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,861 other subscribers

Archive for the ‘C# 3.0’ Category

.NET/C# – opposite of Path.Combine: remove trailing DirectorySeparatorChar

Posted by jpluimers on 2014/01/01

For a project, I needed to strip the potential Path.DirectorySeparatorChar and Path.AltDirectorySeparatorChar.

Where Path.Combine will combine two paths and insert the DirectorySeparatorChar, I could not find the opposite, so I wrote this little piece of code:

using System.IO;

namespace BeSharp.IO
{
    public class PathHelper
    {
        public static string RemoveTrailingDirectorySeparators(string directory)
        {
            string result = directory.TrimEnd(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
            return result;
        }
    }
}

–jeroen

via:

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

.NET/C#: System.Environment.GetLogicalDrives Method is identical to System.IO.Directory.GetLogicalDrives

Posted by jpluimers on 2013/12/26

My mental association with getting LogicalDrives was always the System.IO namespace, so I’ve always used Directory.GetLogicalDrives Method (System.IO) .

Recently I bumped into Environment.GetLogicalDrives Method (System), and discovered it has been available for the same time: since .NET 1.x.

I was not so much amazed that these methods return exactly the same data, but that they have identical code. Not just a single call to some common code: their code is the same, line by line. In .NET 4, they have the code below. Read the rest of this entry »

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

.net – xcopy ASP.NET / WinForms deployment: find common location to access relative files to it (via: Stack Overflow)

Posted by jpluimers on 2013/12/25

StackOverflow user Joe (sorry, no last name) helped me big time by answering my question on Business logic shared by ASP.NET / WinForms: find the location of the assembly to access relative files to it.

Before showing the code at the bottom of this blog post, let me explain the question in more detail:

Basically I was in the midst of refactoring some ‘inherited’ business logic code that – before refactoring – for the ASP.NET side needs to be initialized with an absolute path, but on the WinForms / WPF side only with a relative path to a GetExecutingAssembly directory.

To ease xcopy deployment, I wanted all configuration settings to be relative. But I hadn’t found a common means for these platforms to obtain a directory usable as a root for accessing relative files.

That way I could put identical settings in both the Web.config and App.config, heck even generate them based on a common fragment, whithout having to hard-code absolute path names.

I knew about Assembly.GetExecutingAssembly, but in ASP.NET that location is not where the web site is (both IIS and the WebDevelopment server make use of temporary locations to store the assemblies).

ASP.NET does have Server.MapPath and HostingEnvironment.MapPath, but I didn’t want to make the business logic depend on ASP.NET.

Joe came up with this solution, which works dandy: Read the rest of this entry »

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, ASP.NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, F#, Prism, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 7.0, VB.NET 7.1, VB.NET 8.0, VB.NET 9.0, Web Development | Leave a Comment »

DelphiSpec Library Announce « Роман.Янковский.me

Posted by jpluimers on 2013/12/22

Cool stuff: DelphiSpec library, inspired by Cucumber. It runs on top of DUnit.

via DelphiSpec Library Announce « Роман.Янковский.me.

A similar one in the .NET realm: SpecFlow – Pragmatic BDD for .NET.

–jeroen

Posted in .NET, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 3.0, C# 4.0, C# 5.0, Delphi, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Development, Software Development | Leave a Comment »

.NET/C#: On my research list is the Mono.Options arguments parser

Posted by jpluimers on 2013/12/17

The Mono.Options single .cs source file seems very well suited for arguments parsing of (especially) console application:

https://github.com/mono/mono/blob/master/mcs/class/Mono.Options/Mono.Options/Options.cs

so it is on my research list, like some other .NET/C# based command line parsing libraries (:

–jeroen

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

Attach attach a debugger to a WebService (via: Stack Overflow)

Posted by jpluimers on 2013/12/10

Some things you have done for ages, are already phrased so nicely, the only thing you can do is quote.

Thanks Dave Coulter:

You can attach the Visual Studio debugger to a process by:

Debug > Attach to Process > Attach Read the rest of this entry »

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development, Visual Studio 11, Visual Studio 2002, Visual Studio 2003, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »

.NET/C#: Some links on validation/guarding/checking

Posted by jpluimers on 2013/12/05

From my link archive:

–jeroen

Posted in .NET, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

.NET/C#: Getting volume free space from UNC path requires PInvoke of GetDiskFreeSpaceEx in Kernel32.dll

Posted by jpluimers on 2013/11/26

For some remote monitoring, I needed to get information on UNC paths.

Though suggested, you cannot do this using the System.IO.DriveInfo class (not through the constructor, nor through the VB.NET FileSystem way) as that is about drives, not UNC paths. The System.IO.DriveInfo constructor clearly indicates it doesn’t work with UNC paths. And if you still try, this is the error you will get:

System.ArgumentException was unhandled
HResult=-2147024809
Message=Object must be a root directory ("C:\") or a drive letter ("C").
Source=mscorlib
StackTrace:
at System.IO.DriveInfo..ctor(String driveName
)

Same for WMI: that only works when the UNC path has already been mapped to a drive letter.

You could do with adding a temporary drive letter but since there is nothing as permanent as a temporary

P/Invoke

The actual solution is based on calling Windows API functions using P/Invoke. Read the rest of this entry »

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, CSV, Development, Missed Schedule, SocialMedia, Software Development, WordPress | Leave a Comment »

.net/C#: you can pass anoymous types to a generic method, but not return them as a method result

Posted by jpluimers on 2013/11/12

You can pass anonymous types to a method, if that method has a generic parameter type. I regularly use this to Resolving a parameter name at runtime using the Generic Type Cache technique described by Rinat Abdullin.

The other way around however is impossible, as return types cannot, as Alexander M. Batishchev writes in his StackOverflow answer:

You can’t.

You can only return object, or container of objects, e.g. IEnumerable

Jared Par does a bit more elaboration:

You cannot type any method in C# to be the explicit type of an anonymous types. They cannot be “named” so to speak and hence cannot appear in metadata signatures.

If you really want to return a value which is an anonymous type there are 2 options

  • Have the return type of the method be System.Object. You can then do evil casting hacks to get a typed value in another method. This is very fragile and I don’t recommend it.
  • Use a generic method and a type inference trick to get the return type correct. This would require a very interesting signature definition for your approach.

Anonymous types were not really meant to be passed around in this fashion. At the point you need to pass them around between your functions in this manner, you’re better off explicitly defining a type.

Bummer (:

–jeroen

via:

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

.NET/C#: Fingerprinting a machine

Posted by jpluimers on 2013/11/05

For my link archive, thanks Alek Davis:

Just keep in mind that ID of the CPU is not always available.

By the way, what are you trying to accomplish? If you want to generate a unique key for a computer instance, check the Generating Unique Key (Finger Print) for a Computer for Licensing Purposes post by Sowkot Osman at Codeproject; it can give you some hints (also read comments).

–jeroen

via: .net – APIs in C# for grabbing CPU IDs and drive/volume serial – Stack Overflow.

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »