我是新來的編碼主題,並希望更詳細地瞭解它。我在MSDN上找到了創建文件夾和文件的例子。該文件的創建是通過使用WriteByte方法完成的。 http://msdn.microsoft.com/en-us/library/as2f1fez.aspx文件編碼:這是否適用於C#的WriteByte方法?
爲方便起見,我直接放在下面的代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CreateFolderFile
{
class Program
{
static void Main(string[] args)
{
// Specify a "currently active folder"
string activeDir = @"c:\testdir2";
//Create a new subfolder under the current active folder
string newPath = System.IO.Path.Combine(activeDir, "mySubDir");
// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);
// Create a new file name. This example generates
// a random string.
string newFileName = System.IO.Path.GetRandomFileName();
// Combine the new file name with the path
newPath = System.IO.Path.Combine(newPath, newFileName);
// Create the file and write to it.
// DANGER: System.IO.File.Create will overwrite the file
// if it already exists. This can occur even with
// random file names.
if (!System.IO.File.Exists(newPath))
{
using (System.IO.FileStream fs = System.IO.File.Create(newPath))
{
for (byte i = 0; i < 100; i++)
{
fs.WriteByte(i);
}
}
}
// Read data back from the file to prove
// that the previous code worked.
try
{
byte[] readBuffer = System.IO.File.ReadAllBytes(newPath);
foreach (byte b in readBuffer)
{
Console.WriteLine(b);
}
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
我也發現了關於這個問題的一個有趣的文章由Joel Spolsky的還有:
絕對最低每一個軟件開發者絕對,積極必須知道Unicode和字符集(沒有藉口!) http://www.joelonsoftware.com/printerFriendly/articles/Unicode.html
我的問題:什麼編碼是由第e WriteByte方法?從我所做的閱讀中,無論您使用什麼,是否真的有可能準確確定文件的編碼? (例如:您發送的一個csv文件,並使用Notepad ++確定編碼)。
想法?
這將使用.NET使用的默認編碼。是的,取決於編碼,你可以確定什麼是使用這是像Notepad ++這樣的程序能夠做到這一點。 –
@Ramhound:不,它不會使用「默認編碼」 - 這個概念根本不適用。 'WriteByte'寫入你請求的字節 - 沒有文字可以用這種或那種方式映射。 –