I just started my adventure with C#. I want to write simple program that will read .bin file with OpenFileDialog, edit part of the file, and save the file with SaveFileDialog.
Unfortunately I have some problems probably because I have to learn a lot. Here is part of my code for reading but I have problems to save same file. Basically I think problem is with starting address and ending address because I don't know how to declare this in write function.
OpenFileDialog ofd = new OpenFileDialog();
private void button1_Click(object sender, EventArgs e)
{
ofd.ShowDialog();
BinaryReader br = new BinaryReader(File.OpenRead(ofd.FileName));
br.Close();
}
SaveFileDialog sfd = new SaveFileDialog();
private void button2_Click(object sender, EventArgs e)
{
sfd.ShowDialog();
BinaryWriter br= new BinaryWriter(File.OpenWrite(sfd.FileName));
br.Close();
}
I want to write same file back the length of file is always 8192 bytes so start from 0x0000 until 0x1FFF.
If I get you correctly, you are trying to read from a file, edit it and save the edited file. I think what you are missing is the actual reading
string s = br.ReadString();
and writing
bw.Write(s);
to the file (bw is BinaryWriter).
check out a simple tutorial on reading and writing with binary reader/writer.
Related
I want to copy some text on textbox2 to a txt file.I want to create a kk.txt file after clicking the button and need to store textbox2 text to that kk file.
Here is the code i tried but it only create kk.txt file and not storing textbox2 data.
private void button6_Click(object sender, EventArgs e)
{
//textBox4.Text +=Clipboard.GetText()+Environment.NewLine;
Clipboard.SetText(textBox2.Text);
System.IO.File.Create(#"C:/Ebaycodes/kk.txt");
string path = #"C:/Ebaycodes/kk.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.Write(textBox2.Text);
sw.Dispose();
}
}
}
could somebody help me to fix this error.
The problem is your if statement, it only runs if the file doesn't exist. You created it, so it does exist, and the if doesn't run. You can change your entire routine to this:
private void button6_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox2.Text);
File.WriteAllText(#"c:\Ebaycodes\kk.txt", textbox2.Text);
}
I'm trying to create a normal HTML editor where it's functions are similar to Windows Notepad. Let's say we've written a file and wanted to save it. So the SaveFileDialog comes up and asking us where we want to save it. Then we modified it, and in Notepad you usually just use Ctrl+S to save it again. And this time, no SaveFileDialog will show up. In other words, the existing file has been overwritten. This is what I wanted to achieve. Here's the code I have so far:
private void toolStripButton2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = default(SaveFileDialog);
string location = null;
string sourcecode = textBox1.Text;
sfd = new SaveFileDialog();
location = sfd.FileName;
if (string.IsNullOrEmpty(sfd.FileName))
{
saveAsToolStripMenuItem_Click(sender, e);
}
else
{
File.Copy(sfd.FileName, Path.Combine(location, Path.GetFileName(sfd.FileName)), true);
}
}
I created simple example and I can't find solution anywhere. I want to save UI textbox data, and open it in another(same) windows form file.
Here is code example:
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
StreamWriter write = new StreamWriter(File.Create(sfd.FileName));
write.Write(txtFirstInput.Text);
write.Write(txtSecondInput.Text);
write.Close();
write.Dispose();
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if(ofd.ShowDialog() == DialogResult.OK)
{
StreamReader read = new StreamReader(File.OpenRead(ofd.FileName));
txtFirstInput.Text = read.ReadLine();
txtSecondInput.Text = read.ReadLine();
read.Close();
read.Dispose();
}
}
The problem I get here is that all inputed data get in first textbox. How to separate it? What is the easiest and most efficient way?
Should I create and use byte buffer (and HxD) for streaming through user input?
Can someone please give me some directions or examples.
Thank you for your time
You use ReadLine to read data back, so you need to use WriteLine when writing data to disk
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
using(StreamWriter write = new StreamWriter(File.Create(sfd.FileName)))
{
write.WriteLine(txtFirstInput.Text);
write.WriteLine(txtSecondInput.Text);
}
}
}
Using Write, the data is written to disk without a line carriage return and so it is just on one single line. When you call ReadLine all the data is read and put on the first textbox.
Also notice that it is always recommended to enclose a IDisposable object like the StreamWriter inside a using statement. This approach will ensure that your Stream is correctly closed and disposed also in case of exceptions. (The same should be applied to the StreamReader below)
I have one TextBox, It has some data.
There is a Button. So When I click the button, "save as " dialog should popup to save the text TextBox Data into a file.
I have tried various ways, but getting errors n error. Here I am giving you brief idea how I am writing code, If I am wrong please make me correct.
Or is there any other way to save TextBox Data into a file od my desired path.
protected void ButtonIDSaveAs_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
fs.Close();
}
}
Thanks
Vivek
SaveDialog.OpenFile creates a new file (overwriting an existing file with the same choosen name) and returns a Stream object that could be used as the constructor parameter for a StreamWriter.
So you could simply write
if (saveFileDialog1.FileName != "")
{
using(StreamWriter sw = new StreamWriter(saveFileDialog1.OpenFile()))
{
sw.Write(TextBox1.Text);
}
}
I have a function that can save a .xml file.
private void buttonSaveXML_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "XML Files|*.xml";
saveFile.Title = "Save a Xml File";
saveFile.ShowDialog();
if (saveFile.FileName != "")
{
FileStream fs = (FileStream)saveFile.OpenFile();
dsVersions.WriteXml(fs);
}
}
What do I add to make a specific path that I want to save it to?
You can control the InitialDirectory so that the user will be "in the right place", but you cannot prevent them from switching directories with SaveFileDialog.
That way, they will be in your default path rather than e.g. on the Desktop.
saveFile.InitialDirectory = #"C:\My\Path" ;
Typically I will save the last directory that the user selected to save files to in application configuration and use the user's last directory as the InitialDirectory.
Use the property InitialDirectory of SaveFileDialog form. For Example add this to your code:
saveFile.InitialDirectory = "C:\\MyXMLs\\";
You can see the Documentation.