Click here to Skip to main content
15,914,222 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
QuestionLooking for friends Pin
The Term Papers16-Nov-16 23:26
professionalThe Term Papers16-Nov-16 23:26 
AnswerRe: Looking for friends PinPopular
ZurdoDev17-Nov-16 1:10
professionalZurdoDev17-Nov-16 1:10 
AnswerRe: Looking for friends Pin
Patrice T20-Nov-16 1:37
mvePatrice T20-Nov-16 1:37 
QuestionCOM Interop and release Pin
Midi_Mick6-Nov-16 23:50
professionalMidi_Mick6-Nov-16 23:50 
QuestionRe: COM Interop and release - Code sample Pin
Midi_Mick7-Nov-16 1:49
professionalMidi_Mick7-Nov-16 1:49 
QuestionVS2008 strange no symbols are loaded message Pin
Andrew__26-Oct-16 0:17
Andrew__26-Oct-16 0:17 
AnswerRe: VS2008 strange no symbols are loaded message Pin
Jochen Arndt26-Oct-16 3:13
professionalJochen Arndt26-Oct-16 3:13 
SuggestionWriting unicode characters to an ASCII INI File (yes, you can!) Pin
Cees Verburg24-Oct-16 1:44
Cees Verburg24-Oct-16 1:44 
I was looking for a way to write unicode characters to an INI file. I stumbled upon some solutions which were not satisfactory: they involved ANSI/UTF-8 files. So here is a workaround.
C#
Module INI
    Private Declare Ansi Function WritePrivateProfileString _
       Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
       (ByVal lpApplicationName As String, _
       ByVal lpKeyName As String, ByVal lpString As String, _
       ByVal lpFileName As String) As Integer

    Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String, _
    ByVal lpKeyName As String, _
    ByVal lpDefault As String, _
    ByVal lpReturnedString As StringBuilder, _
    ByVal nSize As Integer, _
    ByVal lpFileName As String) As Integer



    Public Function GetPrivateProfileStringUnicode(ApplicationName As String, KeyName As String, FileName As String) As String
        Dim sb As New StringBuilder(500)
        GetPrivateProfileString(ApplicationName, KeyName, "", sb, sb.Capacity, FileName)
        Dim Value As String = sb.ToString
        If InStr(Value, "#") = 0 Then
            'No #-character found, so there is nothing to convert back
            Return Value
        Else
            Dim i As Integer
            Dim Parts() As String = Split(Value, "#") 'Split value to array
            For i = 1 To UBound(Parts) Step 2         'If i is an odd number Parts(i) always contains a integer which should be converted back
                Parts(i) = Trim(ChrW(CInt(Parts(i))))
            Next
            Return Join(Parts, "")                    'Return the joined array Parts
        End If
    End Function

    Public Sub WritePrivateProfileStringUnicode(ApplicationName As String, KeyName As String, Value As String, FileName As String)
        'First escape the #-character

        Dim NewValue As String = Replace(Value, "#", "#" & Asc("#") & "#")


        If IsUnicode(NewValue) Then
            'Value has unicode characters; we convert them to integer value preceded and followed by a #-character
            Dim i As Integer
            Dim ValueUnicode As String = ""
            For i = 1 To Len(NewValue)
                Dim strChar As String = Mid(NewValue, i, 1)
                If AscW(strChar) > 255 Or AscW(strChar) < 0 Then
                    strChar = "#" & AscW(strChar) & "#"
                End If
                ValueUnicode = ValueUnicode & strChar
            Next
            'Write converted string to INI file directly
            WritePrivateProfileString(ApplicationName, KeyName, ValueUnicode, FileName)
        Else
            'No unicode characters, so write to INI file directly
            WritePrivateProfileString(ApplicationName, KeyName, NewValue, FileName)
        End If
    End Sub

    Private Function IsUnicode(input As String) As Boolean
        Dim asciiBytesCount = Encoding.ASCII.GetByteCount(input)
        Dim unicodBytesCount = Encoding.UTF8.GetByteCount(input)
        Return asciiBytesCount <> unicodBytesCount
    End Function

End Module

GeneralRe: Writing unicode characters to an ASCII INI File (yes, you can!) Pin
Richard MacCutchan24-Oct-16 1:49
mveRichard MacCutchan24-Oct-16 1:49 
GeneralRe: Writing unicode characters to an ASCII INI File (yes, you can!) Pin
Cees Verburg24-Oct-16 8:21
Cees Verburg24-Oct-16 8:21 
GeneralRe: Writing unicode characters to an ASCII INI File (yes, you can!) Pin
NotPolitcallyCorrect24-Oct-16 8:29
NotPolitcallyCorrect24-Oct-16 8:29 
GeneralRe: Writing unicode characters to an ASCII INI File (yes, you can!) Pin
Cees Verburg24-Oct-16 8:45
Cees Verburg24-Oct-16 8:45 
GeneralRe: Writing unicode characters to an ASCII INI File (yes, you can!) Pin
Eddy Vluggen24-Oct-16 9:05
professionalEddy Vluggen24-Oct-16 9:05 
GeneralRe: Writing unicode characters to an ASCII INI File (yes, you can!) Pin
NotPolitcallyCorrect24-Oct-16 10:11
NotPolitcallyCorrect24-Oct-16 10:11 
GeneralRe: Writing unicode characters to an ASCII INI File (yes, you can!) Pin
Gerry Schmitz24-Oct-16 11:54
mveGerry Schmitz24-Oct-16 11:54 
GeneralRe: Writing unicode characters to an ASCII INI File (yes, you can!) Pin
Richard MacCutchan24-Oct-16 21:44
mveRichard MacCutchan24-Oct-16 21:44 
GeneralRe: Writing unicode characters to an ASCII INI File (yes, you can!) Pin
NotPolitcallyCorrect25-Oct-16 2:34
NotPolitcallyCorrect25-Oct-16 2:34 
SuggestionRe: Writing unicode characters to an ASCII INI File (yes, you can!) Pin
ZurdoDev25-Oct-16 3:37
professionalZurdoDev25-Oct-16 3:37 
GeneralRe: Writing unicode characters to an ASCII INI File (yes, you can!) Pin
Gerry Schmitz25-Oct-16 4:50
mveGerry Schmitz25-Oct-16 4:50 
QuestionHOW TO SIGN XML FILE IN .NET COMPACT EDITION Pin
AlexB4717-Oct-16 19:29
AlexB4717-Oct-16 19:29 
Question"The feature you are trying to use ..." Error Pin
jszabo16-Oct-16 12:21
jszabo16-Oct-16 12:21 
AnswerRe: "The feature you are trying to use ..." Error Pin
Dave Kreskowiak16-Oct-16 13:33
mveDave Kreskowiak16-Oct-16 13:33 
GeneralRe: "The feature you are trying to use ..." Error Pin
jszabo17-Oct-16 12:24
jszabo17-Oct-16 12:24 
GeneralRe: "The feature you are trying to use ..." Error Pin
Dave Kreskowiak17-Oct-16 13:10
mveDave Kreskowiak17-Oct-16 13:10 
Question.net freelancer Pin
Member 1278822911-Oct-16 11:06
Member 1278822911-Oct-16 11:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.