dar*_*pbj 94 c# base64 system.io.fileinfo streamreader
标题说明了一切:
我可以确认这两个文件大小相同(以下方法返回true)但我无法再提取副本版本.
我错过了什么吗?
Boolean MyMethod(){
using (StreamReader sr = new StreamReader("C:\...\file.tar.gz")) {
String AsString = sr.ReadToEnd();
byte[] AsBytes = new byte[AsString.Length];
Buffer.BlockCopy(AsString.ToCharArray(), 0, AsBytes, 0, AsBytes.Length);
String AsBase64String = Convert.ToBase64String(AsBytes);
byte[] tempBytes = Convert.FromBase64String(AsBase64String);
File.WriteAllBytes(@"C:\...\file_copy.tar.gz", tempBytes);
}
FileInfo orig = new FileInfo("C:\...\file.tar.gz");
FileInfo copy = new FileInfo("C:\...\file_copy.tar.gz");
// Confirm that both original and copy file have the same number of bytes
return (orig.Length) == (copy.Length);
}
Run Code Online (Sandbox Code Playgroud)
编辑:工作示例更简单(感谢@TS):
Boolean MyMethod(){
byte[] AsBytes = File.ReadAllBytes(@"C:\...\file.tar.gz");
String AsBase64String = Convert.ToBase64String(AsBytes);
byte[] tempBytes = Convert.FromBase64String(AsBase64String);
File.WriteAllBytes(@"C:\...\file_copy.tar.gz", tempBytes);
FileInfo orig = new FileInfo(@"C:\...\file.tar.gz");
FileInfo copy = new FileInfo(@"C:\...\file_copy.tar.gz");
// Confirm that both original and copy file have the same number of bytes
return (orig.Length) == (copy.Length);
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
T.S*_*.S. 248
如果由于某种原因需要将文件转换为base-64字符串.就像你想通过互联网等传递它...你可以做到这一点
Byte[] bytes = File.ReadAllBytes("path");
String file = Convert.ToBase64String(bytes);
Run Code Online (Sandbox Code Playgroud)
相应地,回读文件:
Byte[] bytes = Convert.FromBase64String(b64Str);
File.WriteAllBytes(path, bytes);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
154984 次 |
最近记录: |