I am getting an error in line player.PlaySync().I am a creating .wav file. It is showing me an error
"The file located at
D:\Seo-app\SEO-Automation-Page-Generation.APP\SEO-Automation-Page-Generation.APP\Download\Autoplay.wav
is not a valid wave file."
var response = client.SynthesizeSpeech(input, voice, config);
int count = 1;
string virtualPath = HttpContext.Server.MapPath("/Download/");
if (!Directory.Exists(virtualPath))
Directory.CreateDirectory(virtualPath);
string fullPath = HttpContext.Server.MapPath("/Download/Autoplay.wav");
string fileNameOnly = Path.GetFileNameWithoutExtension(fullPath);
string extension = Path.GetExtension(fullPath);
string path = Path.GetDirectoryName(fullPath);
while (System.IO.File.Exists(fullPath))
{
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
fullPath = Path.Combine(path, tempFileName + extension);
}
using (Stream output = System.IO.File.Create(fullPath))
{
response.AudioContent.WriteTo(output);
}
using (SoundPlayer player = new SoundPlayer())
{
player.SoundLocation = HttpContext.Server.MapPath("/Download/Autoplay.wav");
player.PlaySync();
}
Related
I need a copy file on folder to another folder and i'm use this
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
foreach (string file in files)
{
folderBrowserDialog1.ShowDialog();
string xx = folderBrowserDialog1.SelectedPath;
folderBrowserDialog1.ShowDialog();
string yy = folderBrowserDialog1.SelectedPath;
File.Copy(xx, yy);
But is not working.
Why?
Try this code.
I assume you want to read a file then write it to new one.
Hope it helps.
string sourceFile;
string newFile = "C:\NewFile\NewFile.txt";
string fileToRead = "C:\ReadFile\ReadFile.txt";
bool overwriteExistingFile = true; //change to false if you want no to overwrite the existing file.
bool isReadSuccess = getDataFromFile(fileToRead, ref sourceFile);
if (isReadSuccess)
{
File.Copy(sourceFile, newFile, overwriteExistingFile);
}
else
{
Console.WriteLine("An error occured :" + sourceFile);
}
//Reader Method you can use this or modify it depending on your needs.
public static bool getDataFromFile(string FileToRead, ref string readMessage)
{
try
{
readMessage = "";
if (!File.Exists(FileToRead))
{
readMessage = "File not found: " + FileToRead;
return false;
}
using (StreamReader r = new StreamReader(FileToRead))
{
readMessage = r.ReadToEnd();
}
return true;
}
catch (Exception ex)
{
readMessage = ex.Message;
return false;
}
}
It seems that you do not use filenames in your source code.
I made an example. File copy function.
public void SaveStockInfoToAnotherFile(string sPath, string dPath, string filename)
{
string sourcePath = sPath;
string destinationPath = dPath;
string sourceFile = System.IO.Path.Combine(sourcePath, filename);
string destinationFile = System.IO.Path.Combine(destinationPath, filename);
if (!System.IO.Directory.Exists(destinationPath))
{
System.IO.Directory.CreateDirectory(destinationPath);
}
System.IO.File.Copy(sourceFile, destinationFile, true);
}
//folderBrowserDialog1.ShowDialog();
//string xx = folderBrowserDialog1.SelectedPath;
//string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
//folderBrowserDialog1.ShowDialog();
//string yy = folderBrowserDialog1.SelectedPath;
//foreach (string file in files)
//{
// File.Copy(xx + "\\" + Path.GetFileName(file), yy + "\\" + Path.GetFileName(file));
HttpPostedFile file = context.Request.Files[j];
string fileName = file.FileName;
string fileExtension = System.IO.Path.GetExtension(filepath + file.FileName);
if (!string.IsNullOrEmpty(fileName)
{
string pathToSave_100 = HttpContext.Current.Server.MapPath(filepath) + fileName + fileExtension;
if (File.Exists(pathToSave_100))
{
File.Delete(pathToSave_100);
file.SaveAs(pathToSave_100);
}
else
{
file.SaveAs(pathToSave_100);
}
}
You can do like this:
string strFileExtension = Path.GetExtension(file.FileName);
I have this code to copy a file to another destination, however I need the users to choose the destination plus the name of file copied is = the old name with the date and hour of my PC ..
string fileToCopy = #"d:\pst\2015.pst";
string destinationDirectoryTemplate = textBox2.Text;
var dirPath = String.Format(destinationDirectoryTemplate, DateTime.UtcNow);
var di = new DirectoryInfo(dirPath);
if (!di.Exists)
{
di.Create();
}
var fileName = Path.GetFileName(fileToCopy);
var targetFilePath = Path.Combine(dirPath, fileName);
File.Copy(fileToCopy, targetFilePath);
This should work
protected void Button3_Click(object sender, EventArgs e)
{
string fileToCopy = #"e:\TestFile.pdf";
string destinationDirectoryTemplate = TextBox1.Text;
var dirPath = string.Format(destinationDirectoryTemplate, DateTime.UtcNow);
var di = new DirectoryInfo(dirPath);
if (!di.Exists)
{ di.Create(); }
var fileName = Path.GetFileNameWithoutExtension(fileToCopy);
var extn = Path.GetExtension(fileToCopy);
var finalname = fileName + " " + string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now) + extn;
var targetFilePath = Path.Combine(dirPath, finalname);
File.Copy(fileToCopy, targetFilePath);
}
Hello I have signature like this:
which is encoded to a DataUrl specifically this string:
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAADICAYAAADGFbfiAAAYlElEQVR4Xu2dC8w1R1nHQSCIgIKVGLmoiLciFwUs... (long string)"
What i want to do is Convert this DataUrl to an PNG Image, and save the image to the device, this is what i am doing so far:
if (newItem.FieldType == FormFieldType.Signature)
{
if (newItem.ItemValue != null)
{
//string completeImageName = Auth.host + "/" + li[i];
string path;
string filename;
string stringName = newItem.ItemValue;
var base64Data = Regex.Match(stringName, #"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
var binData = Convert.FromBase64String(base64Data);
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
filename = Path.Combine(path, base64Data);
if (!File.Exists(filename))
{
using (var stream = new MemoryStream(binData))
{
//Code crashing here--------------------------
File.WriteAllBytes(filename, binData);
}
}
newItem.ItemValue = filename;
}
}
App.Database.SaveReportItem(newItem);
But my code is making my application to crash specifically in this line:
File.WriteAllBytes(filename, binData);
The sample I am using as reference (Link) is using a PictureBox but with Xamarin there is no use of a pictureBox.
Any Ideas?
As #SLaks mentioned I didn't need a MemoryStream, the problem with my code was the path and the filename for further help this is the working code:
if (newItem.FieldType == FormFieldType.Signature)
{
if (newItem.ItemValue != null)
{
//string completeImageName = Auth.host + "/" + li[i];
string path;
string filename;
string stringName = newItem.ItemValue;
var base64Data = Regex.Match(stringName, #"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
var binData = Convert.FromBase64String(base64Data);
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//filename = Path.Combine(path, base64Data.Replace(#"/", string.Empty));
long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
string fileName = "Sn" + milliseconds.ToString() + ".PNG";
filename = Path.Combine(path, fileName);
if (!File.Exists(filename))
{
//using (var stream = new MemoryStream(binData))
//{
File.WriteAllBytes(filename, binData);
//}
}
newItem.ItemValue = filename;
}
}
App.Database.SaveReportItem(newItem);
And the image showed:
I just cleaned Mario's code and fine tuned regex:
public string SaveDataUrlToFile(string dataUrl, string savePath)
{
var matchGroups = Regex.Match(dataUrl, #"^data:((?<type>[\w\/]+))?;base64,(?<data>.+)$").Groups;
var base64Data = matchGroups["data"].Value;
var binData = Convert.FromBase64String(base64Data);
System.IO.File.WriteAllBytes(savePath, binData);
return savePath;
}
I am creating a project to do with some simple tasks to help my work.
The application is being made as a windows form in C#.
Everything seems to be fine other than one issue, the issue is that when copying files from a shared location to a local location, all of them work other than one.
the code is as follows:
private void Form1_Load_1(object sender, EventArgs e)
{
string fileName = "Analyst's Name.txt";
string fileName1 = "Group Name.txt";
string fileName2 = "KB Number.txt";
string fileName3 = "Return Notes.txt";
string sourcePath = #"\\remote location";
string targetPath = #"C:\SUPPORT\";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string sourceFile1 = System.IO.Path.Combine(sourcePath, fileName1);
string sourceFile2 = System.IO.Path.Combine(sourcePath, fileName2);
string sourceFile3 = System.IO.Path.Combine(sourcePath, fileName3);
string destFile = System.IO.Path.Combine(targetPath, fileName);
string destFile1 = System.IO.Path.Combine(targetPath, fileName1);
string destFile2 = System.IO.Path.Combine(targetPath, fileName2);
string destFile3 = System.IO.Path.Combine(targetPath, fileName3);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);
System.IO.File.Copy(sourceFile1, destFile1, true);
System.IO.File.Copy(sourceFile2, destFile2, true);
System.IO.File.Copy(sourceFile3, destFile3, true);
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
foreach (string s in files)
{
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
destFile1 = System.IO.Path.Combine(targetPath, fileName1);
destFile2 = System.IO.Path.Combine(targetPath, fileName2);
destFile3 = System.IO.Path.Combine(targetPath, fileName3);
System.IO.File.Copy(s, destFile, true);
System.IO.File.Copy(s, destFile1, true);
System.IO.File.Copy(s, destFile2, true);
System.IO.File.Copy(s, destFile3, true);
now the problem is that the return notes file that is locally on the C drive, is being copied but the data within it is showing as the analyst name data, the name of the file is correct, but the data inside is from a different file, any help would be appreciated.
It should be as simple as this:
List<string> files = new List<string>
{
"Analyst's Name.txt",
"Group Name.txt",
"KB Number.txt",
"Return Notes.txt"
};
string sourcePath = #"\\remote location";
string targetPath = #"C:\SUPPORT\";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
for (int i = 0; i < files.Count; i++)
{
string filePath = Path.Combine(sourcePath, files[i]);
if (File.Exists(filePath))
{
File.Copy(filePath, Path.Combine(targetPath, files[i]), true);
}
}