locked
CreateObject equivalent function in csharp RRS feed

  • Question

  • Using the Microsoft.VisualBasic dll, is it possible to do the following that was originally done in VB:

    Set fso = CreateObject("Scripting.FileSystemObject")
    somevalue= left(fso.GetTempName,len(fso.GetTempName)-4)

    The I ask is because using the VB  code above a unique ID was being generated and it has now become the standard on how this ID is formatted. I need to mimic this in C#.

    Wednesday, April 17, 2013 3:14 PM

Answers

  • You can use dynamic with Activator.CreateInstance to handle this.  This will look something like:

    dynamic fso = Activator.CreateInstance(Type.GetTypeFromProgID("Scripting.FileSystemObject"));
    string someValue = System.IO.Path.GetFilenameWithoutExtension(fso.GetTempName);

    That being said, in this case, I'd just use the framework types:

    string tempFile = Path.GetFilenameWithoutExtension(Path.GetTempFileName());

    In .NET (C#), there's almost always a way to get there with the framework types, and it's often easier to understand.

    For details on dynamic with COM, see: http://www.codeproject.com/Articles/148959/How-the-new-C-dynamic-type-can-simplify-access-to


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Noobville Wednesday, April 17, 2013 4:11 PM
    Wednesday, April 17, 2013 4:04 PM

All replies

  • Hi,

    You could use Activator.CreateInstance. See http://www.novicksoftware.com/TipsAndTricks/tip-csharp-create-com-object-by-progid.htm

    Or http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.interaction.createobject.aspx is still available.

    Before I would recommend to double check if http://msdn.microsoft.com/en-us/library/system.io.path.aspx and the GetTempFileName method wouldn't be usable (both might call the same underlying temp name generation Windows function or in the worst case it could be perhaps close enoguh to avoid using still some external object).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Wednesday, April 17, 2013 4:03 PM
  • You can use dynamic with Activator.CreateInstance to handle this.  This will look something like:

    dynamic fso = Activator.CreateInstance(Type.GetTypeFromProgID("Scripting.FileSystemObject"));
    string someValue = System.IO.Path.GetFilenameWithoutExtension(fso.GetTempName);

    That being said, in this case, I'd just use the framework types:

    string tempFile = Path.GetFilenameWithoutExtension(Path.GetTempFileName());

    In .NET (C#), there's almost always a way to get there with the framework types, and it's often easier to understand.

    For details on dynamic with COM, see: http://www.codeproject.com/Articles/148959/How-the-new-C-dynamic-type-can-simplify-access-to


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Noobville Wednesday, April 17, 2013 4:11 PM
    Wednesday, April 17, 2013 4:04 PM
  • Thanks! I think Reed's more closely yields the value I needed to make the format of the ID I needed; e.g. "rad0D3E2". Thanks for the information though.
    Wednesday, April 17, 2013 4:10 PM
  • The below solution best works for me, Thanks!

    dynamic fso = Activator.CreateInstance(Type.GetTypeFromProgID("Scripting.FileSystemObject"));
                string someValue = System.IO.Path.GetFileNameWithoutExtension(fso.GetTempName);

    • Marked as answer by Noobville Wednesday, April 17, 2013 4:11 PM
    • Unmarked as answer by Noobville Wednesday, April 17, 2013 4:11 PM
    Wednesday, April 17, 2013 4:11 PM
  • Quick question though. Will the resultant value yielded always be unique? E.g. "radF2025"

    If not that is fine, I will have to do some checking on my end if it is not.

    Wednesday, April 17, 2013 4:15 PM
  • Quick question though. Will the resultant value yielded always be unique? E.g. "radF2025"

    If not that is fine, I will have to do some checking on my end if it is not.

    I would use Path.GetTempFilename (as I suggested), as the documentation specifies that it will always "Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file."  Using that method, you don't need COM (will be easier/nicer) and you'll get the uniqueness you need.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, April 17, 2013 4:35 PM
  • Thanks for the information! This is exactly what I needed to hear.
    Wednesday, April 17, 2013 5:01 PM