Read/Write Japanese csv file (edit)
- 932: Shift-JIS on Windows
- System.Text.Encoding.GetEncoding(932)
- System.Text.Encoding.UTF8
- Read/Write csv using Microsoft.VisualBasic.FileIO.TextFieldParser class
- Half-size vs Full-size
- Half-width vs Full-width
- Half-width katakana to Full-width katakana
C# - Convert single byte character string (half width) to double byte (full width) - Stack Overflow
Input Japanese characters into CSV file (C#) - Stack Overflow
C# - How to write Unicode characters to the console? - Stack Overflow
How to use unicode characters in Windows command line? - Stack Overflow
How to write Unicode characters to the console?如何将Unicode字符写入控制台? (timeglobal.cn)
Multi Languages Website - @manhng
System.Text.Encoding.UTF8
System.Text.Encoding.GetEncoding(932)
System.IO.StreamWriter swCsv = new System.IO.StreamWriter(csvFileName, false, System.Text.Encoding.UTF8);
System.IO.StreamWriter swCsv = new System.IO.StreamWriter(csvFileName, true, System.Text.Encoding.UTF8);
using (var streamWriter = new System.IO.StreamWriter($@"{System.Environment.CurrentDirectory}\Data.csv", false, System.Text.Encoding.GetEncoding(932))) { var sb = new System.Text.StringBuilder(); sb.AppendLine("Id,Name"); sb.AppendLine("1,Manh"); sb.AppendLine("2,Van"); streamWriter.Write(sb.ToString()); } using (var readFile = new System.IO.StreamReader($@"{System.Environment.CurrentDirectory}\Data.csv", System.Text.Encoding.GetEncoding(932))) { string line = string.Empty; while ((line = readFile.ReadLine()) != null) { Console.WriteLine(line); } }
Excel failed to display Japanese characters when opening csv file saved from Statistica 13.2 with UTF-8 encoding
Read .CSV file
Namespace: Microsoft.VisualBasic.FileIO
Assembly: Microsoft.VisualBasic.dll
Namespace: Microsoft.VisualBasic.FileIO
Assembly: Microsoft.VisualBasic.Core.dll
TextFieldParser.ReadLine Method (Microsoft.VisualBasic.FileIO) | Microsoft Docs (.NET Framework 4.5.2)
TextFieldParser.ReadLine Method (Microsoft.VisualBasic.FileIO) | Microsoft Docs (.NET Core 6.0)
Microsoft.VisualBasic.FileIO.TextFieldParser.cs
NuGet Gallery | Microsoft.VisualBasic 10.3.0
Comparision
Microsoft.VisualBasic.FileIO.TextFieldParser vs String.Split (github.com)
Samples
C# Read CSV file in .NET Core -TextFieldParser | TheCodeBuzz
C# TextFieldParser Examples: Read CSV - Dot Net Perls
Constants
It's not totally clear what your question is, but if the values are truly constant, I don't see a problem with the simple option of:
public static class LocationConstants
{
public const string StateId = "ST";
public const string CountryId = "CI";
}
Using static in the class declaration signals your intention for the purpose of this class.
Marc Gravell describes some of the potential issues with constants in this Stack Overflow answer. Only you will know if these are a problem in your codebase, but if the values could ever change, use public static readonly instead of const, else any code referring to the constants will need to be rebuilt if the values change.
Compare 'static readonly' vs. 'const'
public static readonly fields are a little unusual; public static properties (with only a get) would be more common (perhaps backed by a private static readonly field).
const values are burned directly into the call-site; this is double edged:
- it is useless if the value is fetched at runtime, perhaps from config
- if you change the value of a const, you need to rebuild all the clients
- but it can be faster, as it avoids a method call...
- ...which might sometimes have been inlined by the JIT anyway
If the value will never change, then const is fine - Zero etc make reasonable consts ;p Other than that, static properties are more common.
Base64String
If you want for some reason to convert your file to base-64 string. Like if you want to pass it via internet, etc... you can do this
Byte[] bytes = File.ReadAllBytes("path");
String file = Convert.ToBase64String(bytes);
And correspondingly, read back to file:
Byte[] bytes = Convert.FromBase64String(b64Str); File.WriteAllBytes(path, bytes);
// Convert from Base64String to Image
public Image LoadImage()
{
//data:image/gif;base64,
//this image is a single pixel (black)
byte[] bytes = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==");
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
return image;
}
bytes = Convert.FromBase64String(file);
var filePath = Server.MapPath("~/Documents/" + fileName); System.IO.File.WriteAllBytes(filePath, bytes);