我正在尝试写出一个Byte[]表示文件完整文件的 数组.
来自客户端的原始文件通过TCP发送,然后由服务器接收.接收到的流被读取到一个字节数组,然后被发送以由该类处理.
这主要是为了确保接收TCPClient为下一个流做好准备并将接收端与处理端分开.
所述FileStream类不采取一个字节数组作为参数或另一个流对象(它允许你写字节到它).
我的目标是通过与原始(使用TCPClient的线程)不同的线程完成处理.
我不知道如何实现这个,我该怎么办?
我们可以使用C#中的内置函数将十六进制字符串转换为字节数组,还是必须为此创建自定义方法?
我正在制作一个 Android 应用程序,一开始应该从 MySQL 数据库加载数据并从服务器加载一些图像。之后,用户将在外面,因此应用程序将离线工作。
我编写了代码来保存所有数据库数据(它有效)和图像从服务器到 ApplicationPersistenceDataPath (不工作)。我已经在寻找我应该如何做到这一点。我的代码不会引发任何错误。但是,当我尝试在应用程序上显示它时,图像是空白的。当时我查看了存储图像的文件夹,但无法打开它们。
这是我保存图像的方法:
IEnumerator loadBgImage(string url, string file_name)
{
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
yield return www.Send();
if (www.isNetworkError || www.isHttpError)
{
print("erro");
}
else
{
string savePath = string.Format("{0}/{1}", Application.persistentDataPath, file_name);
if (!File.Exists(savePath))
{
System.IO.File.WriteAllText(savePath, www.downloadHandler.text);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是在图像中显示图像文件的代码:
string path = Application.persistentDataPath + "/" + nomeImagem;
imagem.GetComponent<SpriteRenderer>().sprite = LoadSprite(path);
Run Code Online (Sandbox Code Playgroud)
这个方法被称为:
private Sprite LoadSprite(string path)
{
if (string.IsNullOrEmpty(path)) return null;
if (System.IO.File.Exists(path))
{
byte[] bytes = File.ReadAllBytes(path);
Texture2D texture = …Run Code Online (Sandbox Code Playgroud) 我想将字节数组附加到现有文件中。它必须位于文件的末尾。我已经可以设法在文件的开头写入。(感谢 stackoverflow ;))。
代码:
public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
{
try
{
// Open file for reading
System.IO.FileStream _FileStream =
new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
System.IO.FileAccess.Write);
// Writes a block of bytes to this stream using data from
// a byte array.
_FileStream.Write(_ByteArray, 0, _ByteArray.Length);
// close file stream
_FileStream.Close();
return true;
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}",
_Exception.ToString());
}
// error occured, return false
return false;
Run Code Online (Sandbox Code Playgroud)
}
从这里得到的:
但我需要它在文件末尾
提前致谢。
找到了解决方案: …
c# ×5
hex ×2
.net ×1
arrays ×1
encoding ×1
file ×1
filestream ×1
filewriter ×1
image ×1
offset ×1