Convert cs. File to exe. File, from another console application program [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a question.
I want my program to create a new cs. File (console application), and than to make it an exe file.
Does it possible? How?
Thanks,
Tom
Edit:
sorry for not being clear. I mean not to rename the file's ending. what i mean is how to make a program that will make an Execution file from a code file (such as cs. files)

.cs file is nothing but a plain text file, written in C# language.
You can use this MSDN reference to make a plain text file:
https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx
As for compiling, it can be done by running csc.exe using proper parameter. MSDN reference here: https://msdn.microsoft.com/en-us/library/78f4aasd.aspx

Your question is not quite clear nor specific but I´ll try to give you some basic information about compiling a C# class.
If you are using a text file you can either use Java-compiler like java from Sun or C# compiler like csc from Microsoft. Although the ending .cs is used frequently it is not necessary in order to compile. Just make sure to have a Main() if you want to make your file executable.
csc myClass.cs
Should do the job in your cmd with the csc compiler. You can execute myClass.exe with:
myClass
You can add /out as an option to change the name of the output file.
However, if you want to compile your class to be used by another class you have to compile it as a module:
csc /t:module myClass.cs
It will be saved as myClass.netmodule.

Related

How do I read this line? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
What exactly does this statement mean?
TextWriter textWriter = new StreamWriter(#System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
I have learned dotnet but haven't worked in any of the development project. Now that I am looking for a job switch, I am making use of some of the sites like Hackerrank. So I just want to know what exactly this statement do and if we omit this sentence what will happen to the code.
glad you are curious and ambitions about probramming.
The following statement
TextWriter textWriter = new StreamWriter(#System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
Simply creates an IO stream that allows you to write to a file on a file system. It is getting the file path that it will be writing to from the environment variable "OUTPUT_PATH" which would need to be setup external from this code.
Presumably the following lines of code will be logging some information to the file.
If you simply omit this line and there were following lines using the local variable textWriter your application would not compile. If you removed all references to this variable nothing would be written to a file.
You should be aware that using a Streamwriter can leave a file open and in use on the file system if you don't dispose of it properly. I would suggest whenever writing to a file to enclose this line in a using statement which will automatically close the file and flush whatever is in the buffer out to the file. Another thing to note is that when writing to files the streamwriter will not automatically "flush" the buffer out to file. This is particularly interesting when you are monitoring an application from files.
For more information on using a testwriter have a look at the MS docs here: https://learn.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=netcore-3.1

Generate C code using C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've got this task that requires me to generate some basic C code using a software written in C#.
The generated code should be based on some input files I provide to my software, we'll call it btOS for easy of communication.
So when starting btOS I give it as input file1, config.xml. When I hit run it should output a file.c that contains some basic structures and/or methods based on what the input files contain.
Is there any elegant way to do this ? Maybe some already generated templates or methods or stuff like that ? The only way I could think of handling this was creating specific strings in C# and outputting them to a C file.
L.E.: It seems that somehow my question was not clear enough. I assume the fault of including C++ in the title, I have remove it but I don't see how that is relevant because the question was very simple.
Anyway, to make it more clear. All i need to do is read some config files (their content is irrelevant, all they contain are some variables that will be used to generate some function templates, which will mostly impact the name of the function) - and write an output file with the extension .C (as in Main.c) that will contain those templates I generated.
So, again, the question: Are there any "elegant" and maybe somehow "professional" ways to do this other than using custom generated strings within the code that I will write to the file ? Right now the only way I see fit to do this without too much hassle is using some template text files with a naming convention defined by me(e.g. function_variableName{...}) where I just change the [variableName] text with whatever I need to to be there and "Abracadabra" I have a function that I will write to the file.
Now as Soonts suggested please try and be helpful, read multiple times if you don't clearly understand or maybe even don't bother - let somebody who is interested in this topic, tries to help or gain some new knowledge before flagging it.
Double Cheers.

Read .Exe file Using c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In c# How many way to open file? Which one is best? and how to open .exe file? Sorry for silly question but i am new in c#.
using (StreamReader srStreamReader = new StreamReader(sString))
{
while ((sline = srStreamReader.ReadLine()) != null)
{
Console.WriteLine(sline);
}
}
I am use this code for this but am not able. so please help
If I understood the problem correctly
You can use like this
string path;
byte[] bufferArray = File.ReadAllBytes(path);
string base64EncodedString = Convert.ToBase64String(bufferArray );
bufferArray = Convert.FromBase64String(base64EncodedString );
File.WriteAllBytes(path, bufferArray );
By open file do you mean execute it or read line by line?
If execute then probably something like this is the answer:
Process.Start("C:\\");
From the code you've provided, it looks like you want to be able to view the source of an .exe. This can't be done without using a decompiler and knowing what the application was compiled with.
If you're trying to execute the .exe file, then take a look at the static method System.Diagnostics.Process.Start(filePath).
If you're trying to actually read the contents, you can use ILSpy or other similar software to decompile the application to view source. ILSpy has source available on GitHub, so you'll be able to use that to get the contents you want.

Is it possible to make a program that reads its own source code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
What I mean is, could one possibly make a program that does the equivalent of
public class PrintOwnSourceCode
{
public static void Main ( )
{
System.Console.WriteLine([something]);
// prints "public class PrintOwnSourceCode { public static void Main ( ) { ... } }"
}
}
???
And would that be an example of reflection?
Somewhat.
Decompilers can do something similar to this:
I just decompiled a decompiler so I could use it to decompile itself
.NET Decompilers, like [.NET Reflector] (http://www.red-gate.com/products/dotnet-development/reflector/) and dotPeek are capable of reflecting upon a .NET assembly and generating files that resemble the source code. It will not look exactly like the source code because compiling and decompiling is kind of like translating English to French and then back to English--the results are not always guaranteed to be 1:1 as Google Translate can demonstrate. Information, like whitespace, that are for easy reading but not required by the compiler will be lost in the decompilation process. So, your application could decompile itself or invoke an external decompiler to print itself.
Aside
In compiled languages, the compiled code does not have direct access to the source code. (Companies don't typically ship the source code with the compiled code to customers. They only ship the compiled executable.) When it comes to parsed languages, like JavaScript, it's a whole different story. Because the source must be available to the runtime so that it can be parsed and run, the code can always find it's own source file, open it, and print it out.
This was answered here.
The short answer is that you cannot print it via reflection.
If you want to print out the file, then you will need to load in the source file itself (and have the file available).

FileNotFoundException on file that exists - C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi
I am trying to read a plain text file named "test.txt" from my system but all the attempts
File.Exists(), StreamReder are not getting the file, that is not a new task for me but i am irritated due to this strange behavior. I have given full permissions to the file but in vain. I am doing the test in a C# console application. The system has a fresh installation and I am wondering of any permission issue when run in debug mode. I also copied the file to Debug folder but still same error. Can anyone please guide me about this? Thanks in advance
There is a neat function in C# for reading string files: (in System.IO namespace)
string text = File.ReadAllText("test.txt");
If you are having trouble with the path, you can add test.txt as a resource with copy to (add the file to teh project, right-click properties and select Copy to output directory.
Then you can use:
string path = Path.Combine(Directory.GetCurrentDirectory(), "test.txt");
File.ReadAllText(path);
Have you stepped through the code? The first step is to make sure the path being used in the program is correct.
Debug mode will still run under your account so, if you have permission to open the file, that won't be a problem.

Categories