c# 等效于 VFP FileToStr()

c# equivalent forVFP FileToStr()

我正在从 VFP 迁移到 c#,需要 FileToStr(path) 方面的帮助,实际上我们有数千个文件存储到数据库中,现在我们想保持与 VFP 的兼容性。

VFP 帮助库中的 FileToStr() 帮助没有说明对文件执行何种编码

FILETOSTR(cFileName) = Returns the contents of a file as a character string.

现在这是一个黑匣子。我有两个问题:

有一个 c# 函数执行与 VFP 相同的任务 FileToStr()?

什么样的编码对文件执行FileToStr()

其实我有这个功能来转换文件:

public async Task<string> ImageToStrAsync(string path)
{
    string BitmapToImage = await Task.Factory.StartNew(() =>
         {
             Bitmap bm = new Bitmap(path);
             TypeConverter cv = TypeDescriptor.GetConverter(typeof(Bitmap));
             return Convert.ToBase64String(
                     (byte[])cv.ConvertTo(bm, typeof(byte[]))
                 );
         });

    return BitmapToImage;
}

另一个之前回答过然后删了...不知道为什么。

您正在寻找的是...

string fileContent = System.IO.File.ReadAllText( someFileNameVariable );

string fileContent = System.IO.File.ReadAllText( someFileNameVariable, System.Text.Encoding.UTF8 );

(或其他编码选项...ASCII、UTF7 等作为枚举选项)。

至于二进制,比如图片文件..

byte[] binaryContent = System.IO.File.ReadAllBytes( someBinaryFileName );

要将您存储的 byte[] 数组(例如位图图像)转换为实际图像对象,您可以执行以下操作。

BitmapImage yourBmp = new BitmapImage();
using (var ms = new System.IO.MemoryStream(binaryContent))
{
    yourBmp.BeginInit();
    yourBmp.CacheOption = BitmapCacheOption.OnLoad;
    yourBmp.StreamSource = ms;
    yourBmp.EndInit();
}

并将字节数组转换为字符串...

string result = System.Text.Encoding.UTF8.GetString(byteArray)