如何使用C#自行提取存档 [英] How do I make a self extracting archive using C#

查看:96
本文介绍了如何使用C#自行提取存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从文件中获取字节然后将它们放入程序然后转换它



process



1.带字节代码的字符串。

2.然后将其转换为exe文件。



请记住该文件需要在没有orgnal文件的情况下自行完成。



我尝试过:



 System.Text.UTF8Encoding enc =  new  System.Text.UTF8Encoding(); 
string str = enc.GetString(fileBytes);
streamWriter f2 = new StreamWriter( bytes .TXT);
f2.Write(str);
f2.Close();
// 我尝试将其设为txt文件,以便将其复制到字符串中
string bit = File.ReadAllText( bytes.txt);
byte [] array = Encoding.ASCII.GetBytes(bit);
File.WriteAllBytes( happy.exe,array);
// 然后我尝试再次将其设为文件,这就是我失败的地方。

解决方案

我很确定你被操作系统视为病毒(创建垃圾exe)。

不要将其作为文本阅读:直接将其作为字节读取:

  byte  [] data = File.ReadAllBytes(pathToFile); 
File.WriteAllBytes(pathToExe,data);



ASCII中的数据只能是7位,不同的系统会以不同的方式解释其中的一些(如\ n和\r在您阅读它们时会被压缩)因此当您将其视为文本时,您可能会丢失重要信息。


引用:

我正在使用C#exe,我正在尝试将其转换为我可以在其他程序上使用的字符串。该文件名为bit.exe。他们没有多少信息



如果我理解,你想在你的程序中嵌入一个随机的exe,并且可能默默地将它作为你的程序运行。

Even如果合法和公平,这将是一项艰巨的任务,你需要深入了解操作系统,因为硬盘上的文件不是内存中的文件,操作系统会对其进行更改,您必须按顺序进行更改运行它。

Quote:

  byte  [] array = Encoding.ASCII.GetBytes(bit); 



在任何情况下,读取字节,写入字节,从不进行转换。


I am trying to get the bytes from a file then put them in the program and then convert it

process

1. string with the bytes code.
2. then convert it to exe file.

Please remember that the file needs to make it self without the orgnal file.

What I have tried:

System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
string str = enc.GetString(fileBytes);
streamWriter f2 = new StreamWriter("bytes.txt");
f2.Write(str);
f2.Close();
// I try to make it a txt file so that i could copy it in to a string
string bit = File.ReadAllText("bytes.txt");
byte[] array = Encoding.ASCII.GetBytes(bit);
File.WriteAllBytes("happy.exe", array);
// then i tried to make it a file again and that is where i falled.

解决方案

I'm pretty sure you are being treated as a "virus" by the OS (creating garbage exe's).


Don't read it as text: read it directly as bytes:

byte[] data = File.ReadAllBytes(pathToFile);
File.WriteAllBytes(pathToExe, data);


Data in ASCII can be just 7 bit, and different systems will interpret some of it in different ways (like \n and \r being "condensed" when you read them for example) so you may lose important information when you treat it as text.


Quote:

i am using C# exe and i am trying to turn it in to a string that i can use on other programs i make. the file is called bit.exe. they not much info then that


If I understand, you want to embed a random exe in your program and probably silently run it as being yours.
Even if legal and fair, it will be a difficult task and you need a deep knowledge about the OS because the file on HDD is not what is run in memory, the os makes changes in it, and you will have to make those changes in order to run it.

Quote:

byte[] array = Encoding.ASCII.GetBytes(bit);


In any case, read bytes, write bytes, never do a conversion.


这篇关于如何使用C#自行提取存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆