How do I read this line? [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 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

Related

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.

Convert cs. File to exe. File, from another console application program [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 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.

How to detect a corrupted file before opening it in 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 7 years ago.
Improve this question
I have some files which have been corrupted and I want to detect which file is corrupted before opening it. I used FileInfor class but useless.
The easiest way to solve this issue is to have your program have a log file of when it accesses and edits a file. By keeping track of this, if a program exited prematurely, you could easily identify the the saving was interrupted. To do this you should have the program add a new log every time the program has completed saving a file, not before it is saved. When the program trys to open the program, you can check the time that the file was last edited and if the last edited time is later than the time logged in the log file, then reject it.
Of course this system will only work on one computer. There are other ways of implementing this such as having a log at the end of the file. If the log does not exist, then you know that the file is corrupt. Open your mind up to more ideas and try to think of some more ways to solve this issue. This is just one example.
1. Unfortunately there is no easy way to determine if file is corrupt before even rendering it. Usually the problem files have a correct header so the real reasons of corruption are different. The file contains a reference table giving the exact byte offset locations of each object from the start of the file. So most probably corrupted files have broken offsets or may have some object missing.
The best way to determine that the file is corrupted is to use specialized libraries of that type like PDF file libraries. There are lots of both free and commercial of such libraries for .NET. You may simply try to load file with one of such libraries. iTextSharp will be a good choice.
2. Or if you want, you can go though this answer :
File Upload Virus Scanning(server side)
You might need to parse the header bytes of the file and make sure it satisfies the rest of the file body contents. e.g.,
Reading image header info without loading the entire image
This is how you can read header of an image and get the image size without opening it. In the same way you should look at the desired file format header and validate it as per the file format rule. This is not the readymade solution but may give you an idea.

How I can read after EOF? 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 8 years ago.
Improve this question
I`m using C# and I need to read something after EOF. Is it possible by using C#? How?
Thanks.
You cant. EOF means end of file, there's nothing actually in the file after that.
You may as well ask how you can get ten gallons of oil from a four-gallon drum. Once it's empty, there's no more to be had.
Since you're talking C# hence Windows (and based on your comment and data located behind the end of file pointer), it's possible that they may be referring to "DOS mode" text files, which are (or used to be, I haven't investigated recently) terminated by the CTRL-Z character.
From the earliest days of the PC revolution, where CP/M used integral numbers of disk blocks to store a file and only stored the number of disk blocks rather than the number of bytes, CTRL-Z was used to indicate end of file if the file wasn't an exact multiple of the disk block size.
If that's the case, it's probably best just to open the file as a binary file, then read up to the first CTRL-Z character (code point 26) - everything beyond that could be considered data beyond EOF if it's truly a text file of that format.

C# Having two programs reading and writing to the same text file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there a way to have the first program writing to the text file, whilst the program waits until the text file is not being used by another process.
You can use the FileSystemWatcher class in the second program, to see when the first program is done. The FileSystemWatcher will trigger an event if the file has been created, modified, or deleted.
More information: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
The best way is to implement a 3rd program which acts as a queue. Your first 2 programs send data to this program, which does the actual writing to the text file.
This 3rd program could be an MSMQ, or a Windows Service, or a set of web-services, or anything - it depends on your requirements...
To prevent the race condition. You will have to implement try-wait-retry option.
Try to read from file
if it fails then wait for a while and try again
Yes. You place each logical write onto the end of a queue. In another thread you read logical writes from the front of the queue and attempt to write them to the file (open file, write, close file); if an IOException occurs during this then you leave the write on the front of the queue, otherwise you remove it from the front of the queue. If both processes following this pattern then they will be able to interleave writes to the file.

Categories