I am writing a text-to-speech program and my program throws an exception (simpleTTS speak error).
How do I solve this problem?
private void Voiced(String textToSpeech)
{
try
{
SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
Voice = new SpVoice();
Voice.Volume = 100;
if (chkSaveToWavFile.Checked)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
sfd.Title = "Save to a wave file";
sfd.FilterIndex = 2;
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream SpFileStream = new SpFileStream();
SpFileStream.Open(sfd.FileName, SpFileMode, false);
Voice.AudioOutputStream = SpFileStream;
Voice.Speak(textToSpeech, SpFlags);
SpFileStream.Close();
}
}
else
{
Voice.Speak(textToSpeech, SpFlags);
}
}
catch (Exception error)
{
MessageBox.Show("Speak error", "SimpleTTS", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Related
I'm trying to do an application in c #, which sends a text file through a serial port to a machine.
all good and beautiful if the machine is put in download mode.
if the operator forgets to put the machine on download mode .... here comes the problem, I have a writetimout exception.
I would like to use this exception or anything else to resume the process and my program will try to reload the file into the machine.
below is my code
private void incarca_button_Click(object sender, EventArgs e)
{
string open_folder = cale_txt.Text.Replace(Environment.NewLine, "");
OpenFileDialog theDialog = new OpenFileDialog();
theDialog.Title = "Open Text File";
theDialog.Filter = "TXT files|*.txt";
theDialog.InitialDirectory = open_folder;
if (theDialog.ShowDialog() == DialogResult.OK)
{
var onlyFileName = System.IO.Path.GetFileName(theDialog.FileName);
piesa_lbl.Hide();
piesa_txt.Hide();
SerialPort cnc_com = new SerialPort(com);
cnc_com.BaudRate = Int32.Parse(bit);
parity = parity.ToString();
cnc_com.Parity = (Parity)Enum.Parse(typeof(Parity), parity);
cnc_com.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stop_bit);
cnc_com.DataBits = Int32.Parse(data_bit);
cnc_com.Handshake = (Handshake)Enum.Parse(typeof(Handshake), flow_control);
cnc_com.Encoding = Encoding.ASCII;
cnc_com.ReadTimeout = 500;
cnc_com.WriteTimeout = 500;
cnc_com.ReadBufferSize = 1000000;
cnc_com.WriteBufferSize = 1000000;
//cnc_com.DtrEnable = true;
//cnc_com.RtsEnable = true;
try
{
cnc_com.Open();
}
catch (UnauthorizedAccessException) { }
catch (System.IO.IOException) { }
catch (ArgumentException) { }
using (StreamReader sr = new StreamReader(open_folder + #"\" + onlyFileName))
{
while (sr.Peek() >= 0)
{
try
{
cnc_com.WriteLine(sr.ReadLine());
//cnc_com.Handshake = (Handshake)Enum.Parse(typeof(Handshake), flow_control);
}
catch(TimeoutException wtout)
{
MessageBox.Show("Masina nu este pregatita\nReincarcati programul");
cnc_com.Close();
cnc_com = null;
}
}
}
piesa_txt.Show();
piesa_lbl.Show();
piesa_txt.Focus();
//cale_txt.Text = "";
cnc_com.DiscardOutBuffer();
cnc_com.DiscardInBuffer();
cnc_com.Close();
cnc_com = null;
}
}
I would like this piece of code to be executed only if the file was passed successfully to the machine
piesa_txt.Show();
piesa_lbl.Show();
piesa_txt.Focus();
//cale_txt.Text = "";
cnc_com.DiscardOutBuffer();
cnc_com.DiscardInBuffer();
cnc_com.Close();
cnc_com = null;
thank you
I'm trying to find a way to have the excel file automatically open as soon as it is done downloading. The file is called "ExportAging.xlsx" and the closest I've gotten to the solution is this:
This is the code I have
private void ExportToExcel()
{
try
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx";
saveDialog.FilterIndex = 1;
saveDialog.FileName = "ExportAging";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
saveDialog.OpenFile();
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
excel.Quit();
workbook = null;
excel = null;
}
}
Any help is appreciated.
So, no need to close the Excel Application and then reopen it:
if (saveDialog.ShowDialog() == DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
excel.Visible = true;
}
Another option can be to close the file after saving it:
if (saveDialog.ShowDialog() == DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
workbook.Close(false);
}
Disposing of Microsoft.Office.Interop.Word.Application
In top of form1 i did
OpenFiledialog openFileDialog1 = new OpenFiledialog();
Then:
private void changeWorkingDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
openFileDialog1.Filter =
"BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|"
+ "All Graphics Types|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff";
openFileDialog1.InitialDirectory = #"c:\";
openFileDialog1.Multiselect = true;
if (result == DialogResult.OK)
{
string[] files = openFileDialog1.FileNames;
try
{
if (files.Length > 0)
{
label6.Text = files.Length.ToString();
label6.Visible = true;
string directoryPath = Path.GetDirectoryName(files[0]);
label12.Text = directoryPath;
label12.Visible = true;
}
}
catch (IOException)
{
}
}
}
But when i click the button the init directory is documents and not c: and i don't see the filter/s at all but i see all the files. It's like the settings i did didn't effect.
ShowDialog is modal, so it waits until the user clicks OK/Cancel.
Only then does the rest of your code run.
You should only call ShowDialog after you have finished setting the properties:
openFileDialog1.Filter = "BMP|*.bmp|GIF|*.gif|JPG|*.jpg...";
openFileDialog1.InitialDirectory = #"c:\";
openFileDialog1.Multiselect = true;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
Your logic is incorrect. You're displaying the dialog before you configure the settings.
// Configure it first
openFileDialog1.Filter = "BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|"
+ "All Graphics Types|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff";
openFileDialog1.InitialDirectory = #"c:\";
openFileDialog1.Multiselect = true;
// Then show it and wait for the user to make a selection or cancel
DialogResult result = openFileDialog1.ShowDialog();
// Take some action if the user made a selection
if (result == DialogResult.OK)
{
...
As a side note, be careful about eating exceptions. At the least, log the error and inform the user.
catch (IOException ex)
{
// log ex.ToString() somewhere
MessageBox.Show("An error has occurred!\r\n\r\n" + ex.Message);
}
The problem is you are calling ShowDialog() before settings the properties, this should work for you:
openFileDialog1.Filter = "BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|"
+ "All Graphics Types|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff";
openFileDialog1.InitialDirectory = #"c:\";
openFileDialog1.Multiselect = true;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string[] files = openFileDialog1.FileNames;
try
{
if (files.Length > 0)
{
label6.Text = files.Length.ToString();
label6.Visible = true;
string directoryPath = Path.GetDirectoryName(files[0]);
label12.Text = directoryPath;
label12.Visible = true;
}
}
catch (IOException)
{
}
}
I'm new to developing, I make a simply app to try to open a file the app return me an error: "access to the path negated" but I'm administrator, UAC is disabled and I'm the only account, why did it give me this error?
Here my code:
private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
string sourceCode = File.ReadAllText(Path.GetDirectoryName(openFileDialog1.FileName));
string colorizedSourceCode = new CodeColorizer().Colorize(sourceCode, Languages.Java);
textBox1.Text = colorizedSourceCode;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
Looks to me like you're trying to open a stream on a folder.
Try replacing this
string sourceCode = File.ReadAllText(Path.GetDirectoryName(openFileDialog1.FileName))
with this:
string sourceCode = File.ReadAllText(openFileDialog1.FileName)
Your opening the stream wrong.
Maybe try something like this?
private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
string strfilename = openFileDialog1.FileName;
string filetext = File.ReadAllText(strfilename);
textBox1.Text = filetext;
button1.PerformClick();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
Im trying to save into SD card and if is not available into save it into Internal memory on the Gallery, but always showing on the Galley directory. But this code is not saving it or how can i do to show it into Gallerys Directory.
string nameFile = file + ".png";
//Check wether the sd card is available or not
string state = Android.OS.Environment.ExternalStorageState;
bool isExternalStorageAvailable = false;
bool isExternalStorageWritable = false;
string path = null;
if(Android.OS.Environment.MediaMounted.Equals(state))
{
isExternalStorageAvailable = isExternalStorageWritable = true;
//var path = Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic);
path = Android.OS.Environment.ExternalStorageDirectory.ToString();
Toast.MakeText(this, "Saved in SDCard", ToastLength.Short).Show();
}
else if(Android.OS.Environment.MediaMountedReadOnly.Equals(state))
{
isExternalStorageAvailable = true;
isExternalStorageWritable = false;
}
else
{
//path = Android.OS.Environment.DataDirectory;
path = Android.OS.Environment.DirectoryPictures;
Toast.MakeText(this, "Saved in Internal Memory ", ToastLength.Short).Show();
}
//Create a Bitmap screen capture
Android.Graphics.Bitmap bitmap;
View view = v.RootView;
view.DrawingCacheEnabled = true;
bitmap = Android.Graphics.Bitmap.CreateBitmap(view.DrawingCache);
view.DrawingCacheEnabled = false;
System.IO.FileStream fs = null;
try
{
fs = new System.IO.FileStream(path + "/" + nameFile, System.IO.FileMode.OpenOrCreate);
bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, fs);
if(isExternalStorageAvailable == true && isExternalStorageWritable == true)
{
//SendBroadcast(new Intent(Intent.ActionMediaMounted, Android.Net.Uri.Parse(path + "/" + nameFile)));
MyMediaConnectorClient client = new MyMediaConnectorClient(path + "/" + nameFile);
MediaScannerConnection scanner = new MediaScannerConnection(this, client);
client.SetScanner(scanner);
scanner.Connect();
}
else
{
Android.Provider.MediaStore.Images.Media.InsertImage(ContentResolver, bitmap, nameFile, v.Id.ToString());
}
fs.Flush();
}
catch(FileNotFoundException e)
{
Log.Error("File Not Found Exception", e.ToString());
}
catch(IOException e)
{
Log.Error("IOException", e.ToString());
}
finally
{
if (fs != null)
fs.Close();
dialog.Dismiss();
}