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.
Related
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
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm developing a program to manage my coin collection. I'd like to export my list of coins in an external file in order to save what I've stored inside the list. As the title says, I'd like to know what is the best way to do that. Should I export the content in a text file, in a Excel file or in a XML file?
I don't know if it is useful to know this, but I'm using LINQ to manage the queries.
At the moment, everything is working as intended. The only thing that I need to finish the project is to save all the data inside the list. I'm not asking for some code to paste, I just want some opinions.
Thanks in advance for the help.
Wow, so many options!
I think these days I prefer JSON. It is lightweight simple, human readable and portable.
With a library such as Newtonsoft then it is also easy. I know you didn't ask for a code example, but below shows how easy it is.
string output = JsonConvert.SerializeObject(myObject, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText("c:\path\outputfile.json", output);
And to read it in again
string json = File.ReadAllText("c:\path\inputfile.json");
MyObject myObject = JsonConvert.DeserializeObject<MyObject >(json);
And if you did want XML, you can use the sample library to then convert your obejct to XML for output
System.Xml.XmlDocument doc = JsonConvert.DeserializeXmlNode(json, "RootElementName");
doc.Save("c:\path\outputfile.xml");
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
Ok let me explain it again
My problem is
I want to display the image. But i want this without the opendialogfile
I tried this:
pictureBox1.Image = Image.FromFile("C:\\Users\\Abdullah\\Documents\\Visual Studio 2013\\Projects\\Maker\\Maker\\add.png");// it works
But i dont want to do this because it will cause errors at deployment time. What i want to do is:
pictureBox1.Image= Image.FromFile("add.png");// because this picture is already in the project folder
In this case it show me error that file not found
Now Hope so I explained it :)
Assuming that you are hard coding the path to your image and the image really exists in that path, then you should remember to escape the backslash when using a constant string like that one.
Try with
pictureBox1.ImageLocation = #"C:\Users\Abdullah\Documents\Visual Studio 2013
\Projects\Maker\Maker\Resources\add.png";
or
pictureBox1.ImageLocation = "C:\\Users\\Abdullah\\Documents\\Visual Studio 2013
\\Projects\\Maker\\Maker\\Resources\\add.png";
(Warning strings splitted in two lines for readability. It should be on one single line)
See How do I write a backslash?
EDIT:
Based on your comment below, then it seems that the Image folder always exists in your project (also when it will be deployed to a customer machine) then you could write something like this
pictureBox1.ImageLocation = Path.Combine(Application.StartupPath, "Images", "add.png");
or
string imageFile = Path.Combine(Application.StartupPath, "Images", "add.png");
pictureBox1.Image= Image.FromFile(imageFile);
But looking back to your example: Is it Images or Resources?
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 9 years ago.
Improve this question
I have a lot of txt files in Resources folder. One of them is corner.txt. I can access this file via this code snippet:
Properties.Resources.corner
I keep file names in string variables. For example:
string fileName = "corner.txt";
I want to access this file via:
Properties.Resources.fileName
Is this possible? How can I access?
I solved the problem this code snippet:
string st = Properties.Resources.ResourceManager.GetString(tableName);
So, I don't use the filename, I use the txt file's string. This is useful for me.
Thanks a lot.
You can use Reflection like that:
var type = typeof(Properties.Resources);
var property = type.GetProperty(fileName, BindingFlags.Static| BindingFlags.NonPublic|BindingFlags.Public);
var value = property.GetValue(null, null);
or use the ResourceManager like that:
value = Properties.Resources.ResourceManager.GetObject(fileName, Properties.Resources.Culture);
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 implementing a Windows application to make planning projects easier at my workplace and I was wondering if there's any clever way of making a txt-file nicely structured.
The application is really very simple, pretty much what it does is give the user a question which is answered in a textbox bellow. The question AND the answer are then both sent to a file but it looks very tacky.
Example:
Question?Answer!Question?Answer!
I would like it to be more like this:
Question?
Answer!
Question?
Answer!
I was also curious about other types files, is it possible to use Pdf or MS word the same way as txt?
You can use File.AppendAllLines() and pass in the different strings as an array. They will appear as separate lines in the text file. You'll also need to add a using System.IO at the top of your file.
For example:
// Ensure file exists before we write
if (!File.Exists("<YOUR_FILE_PATH>.txt"))
{
using (File.CreateText("<YOUR_FILE_PATH>.txt")) {}
}
File.AppendAllLines("<YOUR_FILE_PATH>.txt", new string[] {
"Question1",
"Answer1",
"Question2",
"Answer2",
"Question3",
"Answer3"
});
I hope this is what you're after - the question is a little vague.
As for Word and PDF files, this is more complex. Here's a link to a StackOverflow question about Word:
How can a Word document be created in C#?
and one about PDF:
Creating pdf files at runtime in c#
For a simple text file you could use
StringBuilder fileData = new StringBuilder();
fileData.AppendLine("Question: blah! blah! blah! blah!");
fileData.AppendLine("Answer: blah! blah! blah! blah!");
FileStream fs = new FileStream("yourFile.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(fileData.ToString());
sw.Flush();
fs.Flush();
fs.Close();
But of course it won't give you that bold question flavor, for that you would have to use something else,
like MS Word interop, to learn that visit here.
I wanted to mention the String.Format function.
suppose, you have your strings question and answer, you could do some
using (var stream = new StreamWriter('myfile.txt', true)) // append=true
stream.Write(String.Format("\n{0}\n{1}\n\n{2}\n",
question,
new String('=',question.Length),
answer);
to get a text file like
Question 1
==========
Answer
Second Question
===============
Answer
You might also want to use String.Trim() on question to get rid of leading and trailing whitespace (question = question.Trim()), so the "underline" effect looks nicely.