PDA

View Full Version : مبتدی: ایجاد فایل تکست Unicode UTF8



mahsa.admin
پنج شنبه 25 آبان 1391, 08:46 صبح
با سلام
من با کد زیر یک خروجی با فرمت txt گرفتم ولی مشکلی که هست حروف فارسی آن در فایل تکست موجود میباشد ولی همین فایل وقتی در برنامه های دیگری اوپن میشود دیگر این حروف فارسی خوانده نمیشود
فکر کنم فایل ایجاد شده یونیک کد نیست چطور میتوانم اینا کارو کنم
Dim fsStream As New FileStream("c:\a.txt", FileMode.Create, FileAccess.ReadWrite)
Dim swWrite As New StreamWriter(fsStream)
swWrite.WriteLine()
swWrite.Close()

Hossis
پنج شنبه 25 آبان 1391, 11:46 صبح
io.file.wrilteAlltext("c:\a.txt",textbox1.text,system.text.encoding.unicode)
باید یک همچین چیزی باشه
الان وی بی رو دم دست ندارم تست کنم

gilsoft
دوشنبه 29 آبان 1391, 10:04 صبح
با سلام
من با کد زیر یک خروجی با فرمت txt گرفتم ولی مشکلی که هست حروف فارسی آن در فایل تکست موجود میباشد ولی همین فایل وقتی در برنامه های دیگری اوپن میشود دیگر این حروف فارسی خوانده نمیشود
فکر کنم فایل ایجاد شده یونیک کد نیست چطور میتوانم اینا کارو کنم
Dim fsStream As New FileStream("c:\a.txt", FileMode.Create, FileAccess.ReadWrite)
Dim swWrite As New StreamWriter(fsStream)
swWrite.WriteLine()
swWrite.Close()

سلام دوست عزیز

یکی از روشها مثل کد زیر هستش :

Dim fs As New FileStream("c:\a.txt", FileMode.Create, FileAccess.ReadWrite)
Dim SW As New StreamWriter(fs, System.Text.Encoding.UTF8)
SW.WriteLine()
SW.Close()

و اما روش دوم :

Dim FileName As String = "c:\a.txt"
RichTextBox1.Text = System.IO.File.ReadAllText(FileName, System.Text.Encoding.ASCII)
System.IO.File.WriteAllText(FileName, RichTextBox1.Text, System.Text.Encoding.UTF8)


حتما Test کنید ...

موفق باشید

gilsoft
دوشنبه 29 آبان 1391, 10:18 صبح
این تابع هم برای چک کردن یک فایل UTF8 استفاده میشه .. که اگه فایل Unicode باشه True و در غیر اینصورت False رو برمی گردونه

Private Function IsUnicodeFile(ByVal FileName As String) As Boolean
IsUnicodeFile = False
Dim fs As System.IO.FileStream = New System.IO.FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read)
If fs.CanSeek Then
Dim BOF As Byte() = New Byte(3) {}
fs.Read(BOF, 0, 4)
If (BOF(0) = &HEF AndAlso BOF(1) = &HBB AndAlso BOF(2) = &HBF) OrElse _
(BOF(0) = &HFF AndAlso BOF(1) = &HFE) OrElse _
(BOF(0) = &HFE AndAlso BOF(1) = &HFF) OrElse _
(BOF(0) = 0 AndAlso BOF(1) = 0 AndAlso BOF(2) = &HFE AndAlso BOF(3) = &HFF) Then

IsUnicodeFile = True
Else
IsUnicodeFile = False
End If
fs.Seek(0, System.IO.SeekOrigin.Begin)
Else
IsUnicodeFile = False
End If
fs.Close() : fs.Dispose()
Return (IsUnicodeFile)
End Function

امیدوارم مشکل شما حل شده باشه ...

gilsoft
دوشنبه 29 آبان 1391, 10:21 صبح
اینم یه مثال از MSDN-VB.NET 2010 :

Imports System
Imports System.IO
Imports System.Text

Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"

' This text is added only once to the file.
If File.Exists(path) = False Then

' Create a file to write to.
Dim createText As String = "Hello and Welcome" + Environment.NewLine
File.WriteAllText(path, createText, Encoding.UTF8)
End If

' This text is always added, making the file longer over time
' if it is not deleted.
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText, Encoding.UTF8)

' Open the file to read from.
Dim readText As String = File.ReadAllText(path)
Console.WriteLine(readText)
End Sub
End Class

این مثال هم از MSDN-Microsoft :

Public Sub UTF8EncodingExample()
' Create a UTF-8 encoding.
Dim utf8 As New UTF8Encoding()

' A Unicode string with two characters outside an 8-bit code range.
Dim unicodeString As String = _
"This unicode string contains two characters " & _
"with codes outside an 8-bit code range, " & _
"Pi (" & ChrW(928) & ") and Sigma (" & ChrW(931) & ")."

Console.WriteLine("Original string:")
Console.WriteLine(unicodeString)

' Encode the string.
Dim encodedBytes As Byte() = utf8.GetBytes(unicodeString)
Console.WriteLine()
Console.WriteLine("Encoded bytes:")

Dim b As Byte
For Each b In encodedBytes
Console.Write("[{0}]", b)
Next b
Console.WriteLine()

' Decode bytes back to string.
' Notice Pi and Sigma characters are still present.
Dim decodedString As String = utf8.GetString(encodedBytes)
Console.WriteLine()
Console.WriteLine("Decoded bytes:")
Console.WriteLine(decodedString)
End Sub

موفق باشید