VB string function equivalent in c#...

Posted on 2009-02-18
Last Modified: 2012-06-27
How do i convert this vb code into c#

strTemp = String(255, Chr$(0))
Question by:ube100
    LVL 39

    Expert Comment

    Like this:

    string strTemp = new string('\0', 255);
    LVL 39

    Expert Comment

    The '\0' part is the equivalent of the the Chr(0) function (ascii character NUL).

    Btw, I assume you meant this as VB code, because the mentioned VB code will not compile (first parameter of constructor cannot be an Integer):

    Dim strTemp As New String(Chr(0), 255)

    Author Comment

    next code is:

    lRet = GetTempPath(255, strTemp)

    API function for the GetTempPath is declared like this:

    Private Declare Function GetTempPath Lib "Kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

    How will I convert this into C#.
    LVL 39

    Accepted Solution

    You don't need API functions old style anymore in .NET. You can use the .NET library functions, which are type safe. In this case:


    If you do not want the "System.IO" in front of it, you can put that in the using headers:

    using System.IO;

    string strTemp = Path.GetTempPath();

    As you can see, you do not need to initialize your variables with nullpadded strings anymore. Quite handy this way, isn't it? And whether you use VB.NET or C#.NET or any other .NET language, you can always use these base libraries.

    Author Closing Comment

    thank very much sir for help!!!
    LVL 39

    Expert Comment

    You're welcome, glad it helped :)

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now