Is it possible to create a c# file in c#?
I want to make one c# script that creates multiple c# files in a designated directory and then writes code in the files it just created. I know it would be easier to just create and write the scripts myself, but I want to create this as some form of security. I want it to be able to encrypt and decrypt itself, scan it's own code for inconsistencies and then create every file it needs from within that one script. So no "unwanted" files can be downloaded along side it or allow anyone to access or modify that script.
The main purpose of the code I'm trying to make is to show people how easy it is for ONE file to find and access private information on a device. But I want to make sure that the code is as safe as possible so no "data parasites" can use my code to steal that same sensitive data.
That and I just think it would be cool to watch a script make more scripts :),
Answer by KittenSnipes · Jan 26, 2018 at 10:31 AM
This is very much possible and I decided to take the time and make you an example script that creates one single script. Hopefully you can build from here mate. Cheers!
//This is the directory where the file will be put
public string directoryLocation = "C://Users//YourUserNameGoesHere//Documents//";
//The new scripts name
public string scriptName = "result";
void Start()
{
//File.WriteAllText(directoryLoc + "NewResult2.txt", string.Empty); ==> //This line I commented out can be used to clear the file
//This writes the new file but it will be empty
using (StreamWriter newWriter = new StreamWriter(directoryLocation + scriptName + ".cs", true))
{
//The rest of this writes what you want your file to contain
newWriter.WriteLine("using UnityEngine;");
//This is an empty line because I like having spaces :D
newWriter.WriteLine();
newWriter.WriteLine("public class " + scriptName + " : MonoBehavior {");
newWriter.WriteLine("}");
}
Debug.Log("Writing Of Script Done!");
}
I saw this question and I thought to myself: "code generation, that is cool". But then I read the part of "encrypt and decrypt itself" and downloading code... I'm sorry, but you need to compile C# code before you run it, and Unity does even more things with it before it becomes an executable. So, no, I don't think this is possible on the run.
Lol that will be on his end all this does is write a new script.
Thanks for the help! I've been doing some research and I've come across a solution... but not for Unity. using $$anonymous$$icrosoft.CSharp and using System.CodeDom.Compiler.
But they require a high version of .net then Unity has. So I might have to start working directly in script and not in Unity :(
Well I wish you good luck on finding some form or way to do it. Sorry I was rude earlier but I do think it is possible but maybe not during actual runtime. $$anonymous$$aybe at the start but with enough time and effort you can do it and hopefully it won’t be laggy. No one likes lag lol so have fun and Cheers mate
Your answer
![](https://koobas.hobune.stream/wayback/20220612151307im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Security with android 1 Answer
Unity's IAP Security alert google play 0 Answers
Creating an application that will run only on one computer. 0 Answers
Security Camera Robotic Movement 1 Answer