Save Wav file after recording it automatically - c#

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);
}

Related

Saving item in textbox as textfile

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();

How to solve my error when using FileStream to save selected PDF from Adobe PDF reader?

I am using Adobe PDF Reader to view scanned paper from my computer and try to save it to database in my windows form application.
I select the PDF file by using open file dialog , Then I need to save it in my database. I am using the following code:
private void btnPDF_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
PDFViewer.src = ofd.FileName;
}
}
private void btnsave_Click(object sender, EventArgs e)
{
MemoryStream msins = new MemoryStream();
MemoryStream msid = new MemoryStream();
pictureInsurance.Image.Save(msins, pictureInsurance.Image.RawFormat);
pictureIDIQAMA.Image.Save(msid, pictureIDIQAMA.Image.RawFormat);
byte[] byteInsurance = msins.ToArray();
byte[] byteId = msid.ToArray();
FileStream fStream = File.OpenRead("C:\\myfile.pdf");
byte[] Doccontents = new byte[fStream.Length];
fStream.Read(Doccontents, 0, (int)fStream.Length);
fStream.Close();
if (update == 1)
{
patient.ADD_PATIENT(textName.Text,
Convert.ToInt32(textAge.Text),
textMobile.Text,
textEmail.Text, textaddress.Text,
Convert.ToInt32(combogender.SelectedValue),
textIDNO.Text,
Convert.ToInt32(comboNat.SelectedValue),
byteInsurance,byteId,Doccontents);
MessageBox.Show("Patient Added Successfully", "ADD PATIENT", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
When i use the fixed path its save the content in the database :
FileStream fStream = File.OpenRead("C:\\myfile.pdf");
but when i use the name of Adobe PDF reader to save the selected PDF file its show the following error
"An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: URI formats are not supported."
This is the code with error :
FileStream fStream = File.OpenRead(PDFViewer.src);
And with this fix path it saved without error:
FileStream fStream = File.OpenRead("C:\\myfile.pdf");
This is the part code for my void parameters calling stored procedure and varbinary column is it correct ?
public void ADD_PATIENT(string DocContent )
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.open();
SqlParameter[] param = new SqlParameter[11];
param[10] = new SqlParameter("#DocContent", SqlDbType.VarBinary);
param[10].Value = DocContent;
DAL.ExecuteCommand("ADD_PATIENT", param);
DAL.close();
}
What is the changes in code i need to do to save selected file instead of PDFViewer.src or how can i use byte[] method and memory stream like images to save PDF in the SQL server database ?
The argument of File.OpenRead() requires a string. If PDFViewer.src is not of the type string but of the type URI (or similar) then you get the problem which you are mentioning. Make sure that PDFViewer.src is a string (containing a valid path and filename) or cast it to a string, if possible:
FileStream fStream = File.OpenRead((PDFViewer.src).ToString());
or
FileStream fStream = File.OpenRead((string)PDFViewer.src);

File Stream file saving

I have an image file in Canvas element that I get in code behind in asp.net. now I want to save it to a folder in my project but file stream always saves it to c drive. What do I do?
[WebMethod()]
public void SaveUser(string imageData)
{
//Create image to local machine.
string fileNameWitPath = path + "4200020789506" + ".png";
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
// Save fileNameWitPath variable to Database.
}
Here is an example of how I save files to an Images folder in my project directory.
var fileName = "4200020789506.png";
var base64String = SOME_REALLY_LONG_STRING;
using (var s = new MemoryStream(Convert.FromBase64String(base64String)))
using (var f = new FileStream(Path.Combine(Server.MapPath("~/Images"), fileName), FileMode.Create, FileAccess.Write))
{
s.CopyTo(f);
}
Here's what I do and it works well. For you, filePath/filename = fileNameWitPath. Do this for each file you have. Hope it works for you. If you need further info, Id be glad to help.
using (var stream = File.Create(filePath + filename))
{
attachment.ContentObject.DecodeTo(stream, cancel.Token);
}
I can only imagine your path variable points to your C:\ drive.
You need to set the path variable equal to the location you want, for instance:
public void SaveUser(string imageData)
{
path = #"C:\YourCustomFolder\"; // your path needs to point to the Directory you want to save
//Create image to local machine.
string fileNameWitPath = path + "4200020789506" + ".png";
//chekc if directory exist, if not, create
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
// Save fileNameWitPath variable to Database.
}
I also included a Check to see if your directory exists, and if it does not, it will create a folder called 'YourCustomFolder' on your C drive, where it will save images.
If you would like to save your image to a folder in your project, I would recommend using Server.MapPath(~/YourFolderInApplication)

Parse a file path with FileChooserDialog GtkSharp

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 );

Not able to convert image to binary format to store it into text 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();

Categories

Resources