how can i save my item on textbox3 as text file without default location
this is my code on button as of now i can save the file in textfile but it has default location
protected void Button2_Click(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter("C:\\Users\\casti\\OneDrive\\Desktop\\Text\\Text.txt");
txt.Write(TextBox3.Text);
txt.Close();
}
Instead of streamwriter you can use filestream to achieve this.
Try something like this.
var filePath = "your destination path";
FileStream fstream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
StreamWriter streamWriter= new StreamWriter(fstream);
streamWriter.BaseStream.Seek(0, SeekOrigin.End);
streamWriter.Write(TextBox3.Text);
streamWriter.Flush();
streamWriter.Close();
Related
I don't want the user to decide which location of the wav file should be saved , and automatically save it in D:\
private void btnAdd_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = #"D:\CIS";
saveFileDialog.FileName = textBox1.Text;
Stream fileStream = saveFileDialog.OpenFile();
this.encoder.Save(fileStream);
}
If you meant you don't want to prompt the user on where to save the file, and rather just save the file directly, just do
using (Stream fileStream = File.Open(Path.Combine(#"D:\CIS",textBox1.Text) , FileMode.Open))
{
this.encoder.Save(fileStream), FileMode.Open);
}
Simply open a new file with the filename that you want and write the data to it like this:
string filename = "d:\\file1.wav";
using (Stream fileStream = File.OpenWrite(filename))
{
this.encoder.Save(fileStream);
}
I am making an Winform Application with SQLite.
I am working with Load Image function into my C# winform Application. At some point user need to add Image and some time user don't need to add images.When form of image load left blank and before saving the form it is showing exception "empty Path name is not legal". Exception occurs on below code
byte[] imageBt = null;
FileStream fstream = new FileStream(this.cnicloc_txt.Text, FileMode.Open,
FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
And my Load Image code is as follows
private void loadImage_btn_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|All Files(*.*)|*.*";
dlg.Title = "Select CNIC of Owner...";
if(dlg.ShowDialog()==DialogResult.OK)
{
string picPath = dlg.FileName.ToString();
cnicloc_txt.Text = picPath;
}}
I am new here please forgive me if I commit any nonsense
It is a good idea to test the values before you pass them to constructors. Test the value of this.cnicloc_txt.Text, if null abort the operation. Do something like this:
string file = this.cnicloc_txt.Text;
if (!string.IsNullOrWhiteSpace(file))
{
byte[] imageBt = null;
FileStream fstream = new FileStream(file, FileMode.Open,
FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
}
Happy coding :)
if (!string.IsNullOrWhiteSpace(cnicloc_txt.Text))
{
if (!File.Exists(cnicloc_txt.Text))
{
MessageBox.Show("The file you entered does not exist.");
return;
}
// load the image....
}
For me, it's better to write it this way..
string file = this.cnicloc_txt.Text.Trim();
if (!string.IsNullOrEmpty(file))
{
byte[] imageBt = null;
FileStream fstream = new FileStream(file, FileMode.Open,
FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
}
I am using a StreamReader in WeatherController.cs to read data form a CSV file. And in my MainWindow i am using a FileChooseDialog to find the file to read from. Like so:
protected void OnButton2Clicked (object sender, EventArgs e)
{
//Open file dialog and choose a file.
Gtk.FileChooserDialog fc=
new Gtk.FileChooserDialog("Choose the file to open",
this,
Gtk.FileChooserAction.Open,
"Cancel",Gtk.ResponseType.Cancel,
"Open",Gtk.ResponseType.Accept);
fc.Filter = new FileFilter ();
fc.Filter.AddPattern ("*.csv");
if (fc.Run() == (int)Gtk.ResponseType.Accept)
{
b_Next.Sensitive = true;
System.IO.FileStream file = System.IO.File.OpenRead(fc.Filename);
file.Close();
}
//Destroy() to close the File Dialog
fc.Destroy();
}
How do i get the file path from this file to be used in my WeatherController.cs StreamReader?
My StreamReader:
using (StreamReader sr = new StreamReader (file))
string folder = Path.GetDirectoryName( file );
I'm trying to convert the image to binary format by using FileStream object as well as File.WriteAllBytes but the text file is empty. Attached the code below.
string fname,sfname; FileStream fsrw; byte[] bytearray;
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnbrowse_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
fname = openFileDialog1.FileName;
pictureBox1.ImageLocation = fname;
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
sfname = openFileDialog1.FileName;
fsrw = new FileStream(sfname, FileMode.Open);
bytearray = new Byte[fsrw.Length];
fsrw.Read(bytearray, 0, Convert.ToInt32(fsrw.Length));
MessageBox.Show("success");
File.write
}
When I put a breakpoint and check the execution the fsrw has the the file in it and when I hover over the bytearray it gives byte[300127] and its all zeros in the array.
You are not reading the image item when you do the following line:
fsrw.Read(bytearray, 0, Convert.ToInt32(fsrw.Length));
In fact, your never set the bytearray so it's 0 valued. And fsrw is your output stream, not your input.
If I understand your code, your image file (input) is located at fname, so you should do:
private void button1_Click(object sender, EventArgs e)
{
// Select the output file
openFileDialog1.ShowDialog();
sfname = openFileDialog1.FileName;
// Create an output stream with this file
fsrw = new FileStream(sfname, FileMode.Open);
// Read your image
bytearray = File.ReadAllBytes(fname);
// Write the array to the outputStream
fsrw.Write(bytearray, 0, bytearray.Length);
fsrw.Close();
MessageBox.Show("success");
}
If you need to save your picture into a new file, just change this method (set the sfname string with the location) and change the new FileStream(sfname, FileMode.Open); in a new FileStream(sfname, FileMode.Create);
Hi use this it is working for me
var sfname = openFileDialog1.FileName;
FileStream stream = new FileStream(sfname, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(stream );
byte[] image = br.ReadBytes((int)stream .Length);
br.Close();
stream .Close();
Alright, I'm at a loss here. I am trying to download a jar file and then make a batch file that runs it. I was able to download this file once before with my code, but now the completed event fires (I think, because the code inside it runs. Sorry I'm new-ish to C#.) instantly but no file is downloaded. I added a new button with nothing but the download code, and it the file was there, but it just showed up as 0KB. Nothing even shows up in Fiddler with my current code. I am using the IP address to skip the DNS checking and I set the proxy to null. (I read somewhere that doing this stops it from hanging. I think. It was something about web proxy auto-detection.) I can verify that the download links (which I don't show here, unless you need it) are real and downloads the file by simply opening it up in a web browser. Anyway, here is a snippet of my code:
WebClient wc1 = new WebClient();
wc1.DownloadFileCompleted += new AsyncCompletedEventHandler(wc1_DownloadFileCompleted);
wc1.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc1_DownloadProgressChanged);
if (cmboboxVersion.SelectedText == ...)
{
stsprgsbar.Style = ProgressBarStyle.Continuous;
stslblStaus.Text = "Downloading files...";
wc1.DownloadFileAsync(new Uri(...), #txtboxFolder.Text + "\\jarfile.jar");
FileStream fs = new FileStream(#txtboxFolder.Text + "\\batfile.bat", FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(...);
sw.Close();
fs.Close();
}
else if (cmboboxVersion.SelectedText == ...)
{
stsprgsbar.Style = ProgressBarStyle.Continuous;
stslblStaus.Text = "Downloading files...";
wc1.DownloadFileAsync(new Uri(...), #txtboxFolder.Text + "\\jarfile.jar");
FileStream fs = new FileStream(#txtboxFolder.Text + "\\batfile.bat", FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(...);
sw.Close();
fs.Close();
}
else
{
stsprgsbar.Style = ProgressBarStyle.Continuous;
stslblStaus.Text = "Downloading files...";
wc1.DownloadFileAsync(new Uri(...), #txtboxFolder.Text + "\\jarfile.jar");
FileStream fs = new FileStream(#txtboxFolder.Text + "\\batfile.bat", FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(...);
sw.Close();
fs.Close();
}
}
public void wc1_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
stsprgsbar.Value = e.ProgressPercentage;
}
public void wc1_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
string BatPath = #txtboxFolder.Text + "\\batfile.bat";
stsprgsbar.Style = ProgressBarStyle.Marquee;
stslblStaus.Text = "Generating files...";
ProcessStartInfo pro = new ProcessStartInfo(BatPath);
//pro.CreateNoWindow = true;
Process.Start(pro);
}
wc1.DownloadFileAsync(new Uri(...), #txtboxFolder.Text + "\\jarfile.jar", #"c:\jarfile.jar"););