I have some problems with relative paths and reproduction of wav files. I have this simple code which works perfectly:
SoundPlayer player = new SoundPlayer();
player.SoundLocation = #"C:\Users\Admin\Documents\Visual Studio 2012\Projects\TestProject\TestProject\Data\Sounds\car.wav";
player.Play();
I want somehow to play that file with relative path but I didn't have success with this:
SoundPlayer player = new SoundPlayer();
player.SoundLocation = #"Data\Sounds\car.wav";
player.Play();
Thank you!
Is Data directory in the root directory of your application? Are you copying the directory contents as output?
If so, did you mean, Data\Sounds\car.wav?
Which, if running from Visual Studio would be in [projectroot]\[release]\bin\Data\Sounds\car.wav
If you don't see this directory in your bin folder, you'll need to ensure you're selecting all of the files you want copied to your output directory (which will copy the directory structure). You can do this by clicking on the file in your project and selecting the file as output.
Get the full path of your file with Path.GetFullPath("relativ/path")
You might be better off using absolute path after all. You can get the root path from the exe file, then append your relative path to it.
Like this:
// getting root path
string rootLocation = typeof(Program).Assembly.Location;
// appending sound location
string fullPathToSound = Path.Combine(rootLocation, #"Data\Sounds\car.wav");
player.SoundLocation = fullPathToSound;
//WindowsFormsApplication4.exe is name of name space this file name found in Debug file
//you should copy your "sound.wav" into your Debug file
string x = (Assembly.GetEntryAssembly().Location + "");
x = x.Replace("WindowsFormsApplication4.exe", "sound.wav");
SoundPlayer player1 = new SoundPlayer(x);
player1.Play();
This is worked for me
System.Media.SoundPlayer player1 = new System.Media.SoundPlayer(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\1.wav");
Related
get current date and make directory and second when directory is created, in that directory I have to store excel file and also save file as current date.
String Todaysdate = DateTime.Now.ToString("dd-MM-yyyy");
if (!Directory.Exists("C:\\Users\\Krupal\\Desktop\\" + Todaysdate))
{
Directory.CreateDirectory("C:\\Users\\Krupal\\Desktop\\" + Todaysdate);
}
This code have made directory with current date.
But when I want to store file in that directory, it generates the error:
Could not find a part of the path
'D:\WORK\RNSB\RNSB\bin\Debug\22-01-2020\22-01-2020.XLS
Belove path is store excel file that i have to store.
using (System.IO.StreamWriter file = new System.IO.StreamWriter(Todaysdate+"\\"+DateTime.Now.ToString("dd/MM/yyyy") +".XLS"))
Actually you are making the directory in a path then you are saving the .xls in another path.
You are making the directory using this path:
"C:\\Users\\Krupal\\Desktop\\" + Todaysdate
Then, here the path where you are trying to save the .xls:
Todaysdate+"\\"+DateTime.Now.ToString("dd/MM/yyyy") +".XLS"
The error shows the problem clearly, it could not fin this path:
D:\WORK\RNSB\RNSB\bin\Debug\22-01-2020\22-01-2020.XLS
While creating the .xls you are omitting the root path, so the process looks for the path 22-01-2020\22-01-2020.XLS in his working directory D:\WORK\RNSB\RNSB\bin\Debug.
You just need to align those paths: I sugget you to use relative paths, so here how you should fix your code:
String Todaysdate = DateTime.Now.ToString("dd-MM-yyyy");
if (!Directory.Exists(Todaysdate))
{
Directory.CreateDirectory(Todaysdate);
}
//then
using (System.IO.StreamWriter file = new System.IO.StreamWriter(Todaysdate+"\\"+DateTime.Now.ToString("dd/MM/yyyy") +".XLS"))
I presume you are running your WinForms application in Debug mode. This means that your current path is [your application path]\bin\Debug. If you look in file explorer, you will find that an executable has been created there. When using StreamWriter without an absolute file name, the file it tries to create is relative to the current execution path (in your case 'D:\WORK\RNSB\RNSB\bin\Debug'). StreamWriter will create a new file, if one does not exist, but it will not create a new folder, and you are passing it Todaysdate + "\\" which is effectively a new folder. Hence you are getting the error message.
To fix your problem, you need to provide the absolute path to your newly created directory thus:
using (System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Users\\Krupal\\Desktop\\" + Todaysdate+"\\"+DateTime.Now.ToString("dd/MM/yyyy") +".XLS"))
Winforms always expect directories inside Debug Folder, since it's EXE file is inside Debug and try to find it inside Debug folder.
In error it clearly shows that it is looking inside "Debug" folder.
Can you check whether File Exists in the mentioned folder created by you in C Drive.
// To Write File
System.IO.File.WriteAllLines(#"C:\Users\Public\TestFolder\WriteLines.txt", lines);
You can follow this MSDN Post, hope it helps, if Yes, please Upvote it
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file
I am trying to upload a file with the sendkeys function on the inputid.
I am currently using the path C:\Users\myusername\Documents\seleniumsolution\Utils\Dir\Dir1\Dir2\Dir3\UploadFolder\example.jpg
I see the file in my solution. But I dont want to give to complete path but only the filename and that selenium finds the path itself. Otherwise I will be the only one that can use this testcase.
I tried with:
var file = Path.Combine(Directory.GetCurrentDirectory(), url);
And tried with:
string documents = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),"UploadFolder");
Both of them dont give me the required result.
Hope that you guys can help me out.
You might need to travel on the folders tree to get to the file
AppDomain.CurrentDomain.BaseDirectory
will get you in the bin\debug folder. From there you can use parent to move up to the file location. For example (might need some tweaking)
string solutionParentDirectory = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.FullName;
string file = Path.Combine(solutionParentDirectory, #"UploadFolder\example.jpg");
You can also try
string file = Path.GetFullPath("example.jpg");
i have this problem, i created an application that play videos but this video haven't extension , so per example i have video like
big_buck_bunny
instead a file like :
big_buck_bunny.mp4
I cant convert this video.
There is a way to play a video from remote without extension?
I have a url like :
D:\\Software\\Marmotta\\marmotta-home\\resources\\88\\95\\43\\big_buck_bunny
Thanks all for help
What about adding the extension at the end of the file?
String path = "D:\\Software\\Marmotta\\marmotta-home\\resources\\88\\95\\43\\big_buck_bunny";
path += #".mp4";
You can use Directory.GetFiles to get all files in a folder filtered to your filename. Be sure to include using System.IO; at thte top of the file.
var path = "D:\\Software\\Marmotta\\marmotta-home\\resources\\88\\95\\43\\big_buck_bunny"
var directory = Path.GetDirectoryName(path);
var filename = Path.GetFileName(path);
string[] files = Directory.GetFiles(directory, filename + ".*", SearchOption.TopDirectoryOnly);
the array files will hold a list of all the files in the folder that started with the name big_buck_bunny, one of those files will be the one you want to use.
I have stored an audio file in the resources folder of my application but when I use the below path I get a file no found exception.
Can someone explain if this is the correct way to reference a file in resources or if I need to set the path differently?
This is the code that take the audio file as a parameter:
SoundPlayer player = new SoundPlayer("Resources/Audio/punchSound.wav");
player.Load();
player.Play();
You can use resource string:
var music = Properties.Resources.punchSound;
And then use SoundPlayer like this:
var player = new SoundPlayer(new MemoryStream(music));
I have this chunk of code:
private void button4_MouseEnter(object sender, MouseEventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"Resources/navigation.wav");
player.Play();
}
And I get FileNotFoundException, but navigation.wav is in Project/Resources. Plese help!!!
This looks for the file from your Bin\Debug Folder
You have couple of options:
Right click the file and pick Properties. Select for BuildAction = Content.
You will find the file under Bin\Debug\Resource\Sound.wav
Right click the file and pick Properties. Select for BuildAction = Embedded Resource.
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "NamespaceName.FolderName.Sound.wav";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
var wave = new WaveFileReader(stream);
Console.WriteLine(wave.TotalTime);
}
The path is determined relatively from the executable, so in this case probably Bin\Debug.
Try to add the resource in your application as Content (it copies the file to Bin\Debug). That should work.
That path is relative to the directory in which the application is currently running. If you hit F5 in Visual Studio this is most probably bin/Debug, so the file should be there.
Consider embedding this resource, or setting Copy to output directory property to "copy always".
To start with, you need a backslash: #"Resources\navigation.wav"
If this doesn't help, then most likely you are running your application from a different directory than you think. Are you running in debug mode from VS? Is your file in Project\bin\Debug\Resources then?