I have one OpenFileDialog control that has Multiselect = true. Now I want to add each file to windows media player playlist but I have no idea how to do that and there is no good example on the internet.
if (ofdSong.ShowDialog() == DialogResult.OK)
{
foreach (string file in ofdSong.FileNames)
{
//Code to add file to the playlist
}
}
With help of DJ KRAZE that gave me the example link and JayJay who wrote that example, here is the solution.
WMPLib.IWMPPlaylist playlist = wmp.playlistCollection.newPlaylist("myplaylist");
WMPLib.IWMPMedia media;
if (ofdSong.ShowDialog() == DialogResult.OK)
{
foreach (string file in ofdSong.FileNames)
{
media = wmp.newMedia(file);
playlist.appendItem(media);
}
}
wmp.currentPlaylist = playlist;
wmp.Ctlcontrols.play();
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
var myPlayList = axWindowsMediaPlayer1.playlistCollection.newPlaylist("MyPlayList");
OpenFileDialog open = new OpenFileDialog();
open.Multiselect =true;
open.Filter = "All Files|*.*";
if(open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach(string file in open.FileNames)
{
var mediaItem = axWindowsMediaPlayer1.newMedia(file);
myPlayList.appendItem(mediaItem);
}
}
axWindowsMediaPlayer1.currentPlaylist = myPlayList;
}
to play multiple items: copy and paste and enjoy
Related
I would like to make a file explorer with Windows Forms, i already have done a few things, but when i would like to use the DoubleClick event of my ListView I dont know how to code that file explorer needs to act differently when I make the doubleclick on a file or a folder.
My goal is:
Clicking on a file - Loads its text into a TextBox
Clicking on a directory - Opens it and loads it into the listview.
I know how to do 1. and 2. as well, I just don't know how can I make my DoubleClick function know what the selected item in ListView was 1. or 2.
I build my ListView like this:
private void miOpen_Click(object sender, EventArgs e)
{
InputDialog dlg = new InputDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
DirectoryInfo parentDI = new DirectoryInfo(dlg.Path);
listView1.Items.Clear();
try
{
foreach (DirectoryInfo df in parentDI.GetDirectories())
{
ListViewItem lvi = new ListViewItem(new string[] {
df.Name, df.Parent.ToString(),
df.CreationTime.ToShortDateString(), df.FullName });
listView1.Items.Add(lvi);
}
foreach (FileInfo fi in parentDI.GetFiles())
{
ListViewItem lvi = new ListViewItem(new string[] {
fi.Name, fi.Length.ToString(),
fi.CreationTime.ToShortDateString(), fi.FullName } );
listView1.Items.Add(lvi);
}
}
catch { }
}
}
Add the DirectoryInfo or the FileInfo objects to the Tag property of the ListViewItem. I.e
...
var lvi = new ListViewItem(new string[] {
df.Name,
df.Parent.ToString(),
df.CreationTime.ToShortDateString(),
df.FullName
});
lvi.Tag = df;
listView1.Items.Add(lvi);
...
or for the file info:
lvi.Tag = fi;
Then, after having selected an item in the listview:
private void btnTest_Click(object sender, EventArgs e)
{
// Show the first item selected as an example.
if (listView1.SelectedItems.Count > 0) {
switch (listView1.SelectedItems[0].Tag) {
case DirectoryInfo di:
MessageBox.Show($"Directory = {di.Name}");
break;
case FileInfo fi:
MessageBox.Show($"File = {fi.Name}");
break;
default:
break;
}
}
}
Try this code:
FileAttributes fileAttributes = File.GetAttributes("C:\\file.txt");
if (fileAttributes.HasFlag(FileAttributes.Directory))
Console.WriteLine("This path is for directory");
else
Console.WriteLine("This path is for file");
I am a complete newbie when it comes to programing. I followed the tutorial here to create a picture box. http://msdn.microsoft.com/en-u s/library/dd492135.aspx
I work in c#. Now i am going through and making some changes. Is there anyway to make a code so that there can be a next and back button?
Any help would be appriciated.
Thanks,
Rehan
If I understand well you want to have two buttons (Next, Back)
I make a little project and I will be happy if it helped you.
If you want this, continue reading:
First we have to declare imageCount and a List<string>: Imagefiles
so we have this
List<string> Imagefiles = new List<string>();
int imageCount = 0;
imageCount helps to change images using buttons
Imagefiles contains all Image paths of our photos
Now to change photos we have to declare a path first which wil contains all photos.
I use FolderBrowserDialog
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
findImagesInDirectory(fbd.SelectedPath);
}
}
You will see that I use findImagesInDirectory and this method doesnt exist. We have to create it.
This method will help us to filter all the files from the path and get only the image files
private void findImagesInDirectory(string path)
{
string[] files = Directory.GetFiles(path);
foreach(string s in files)
{
if (s.EndsWith(".jpg") || s.EndsWith(".png")) //add more format files here
{
Imagefiles.Add(s);
}
}
try
{
pictureBox1.ImageLocation = Imagefiles.First();
}
catch { MessageBox.Show("No files found!"); }
}
I use try because if no image files, with the above extension, exists code will break.
Now we declare all Image files (if they exists)
Next Button
private void nextImageBtn_Click(object sender, EventArgs e)
{
if (imageCount + 1 == Imagefiles.Count)
{
MessageBox.Show("No Other Images!");
}
else
{
string nextImage = Imagefiles[imageCount + 1];
pictureBox1.ImageLocation = nextImage;
imageCount += 1;
}
}
Previous Button
private void prevImageBtn_Click(object sender, EventArgs e)
{
if(imageCount == 0)
{
MessageBox.Show("No Other Images!");
}
else
{
string prevImage = Imagefiles[imageCount -1];
pictureBox1.ImageLocation = prevImage;
imageCount -= 1;
}
}
I believe my code help. Hope no bugs!
I'm trying to create a file, add all the listbox items to the file. SO I can later on open the file and show all the listbox items again.
My current code is not working, it wont create a file or save to a existing file.
Function to get the name of thefile created / path
private void mnuFileSaveAs_Click(object sender, EventArgs e)
{
string fileName = "";
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
if(fileName == String.Empty)
{
mnuFileSaveAs_Click(sender, e);
}
else
{
fileName = sfd.FileName;
writeToFile(fileName);
}
}
}
Function to write to file
private void writeToFile(string fileName)
{
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(fileName);
foreach (var item in listBox.Items)
{
SaveFile.WriteLine(item.ToString());
}
}
Well you didn't specify the error, but my guess is that it isn't working because you didn't close the StreamWriter.
using (System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(fileName))
{
foreach (var item in listBox.Items)
SaveFile.WriteLine(item.ToString());
}
Or you can just call SaveFile.Close() instead of using
I have this in my designer:
On the right is the files in a listView1.
On the left is the directory main directory of this files treeView1.
I have this code in menu strip item clicked event :
void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Upload")
{
List<String> selected = new List<String>();
foreach (ListViewItem lvi in listView1.SelectedItems)
{
selected.Add(lvi.Text);
}
AllFiles = selected.ToArray();
Bgw.RunWorkerAsync();
}
}
The problem is that the file/s in AllFiles array are only the filenames for example: bootmgr or install.exe
But i need that in the All Files each file will have also it's full path for example:
c:\bootmgr or c:\install.exe or c:\test\test\example.txt
How can i add to AllFiles also the paths ?
I tried now:
void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Upload")
{
List<String> selected = new List<String>();
string dir = treeView1.SelectedNode.FullPath;
foreach (ListViewItem lvi in listView1.SelectedItems)
{
string g = Path.Combine(dir, lvi.Text);
selected.Add(g);
}
AllFiles = selected.ToArray();
Bgw.RunWorkerAsync();
}
}
And in form1:
private void FtpProgress_DoWork(object sender, DoWorkEventArgs e)
{
f = new FtpSettings();
f.Host = "ftP://ftp.newsxpressmedia.com";
f.Username = "...";
f.Password = "...";
files = TV_LV_Basic.ExplorerTree.AllFiles;
StringArrayUploadFiles(sender, e);
}
AllFiles contain the files and paths for example C:\test.txt
Then :
private void StringArrayUploadFiles(object sender, DoWorkEventArgs e)
{
try
{
foreach (string txf in files)
{
string fn = txf;
BackgroundWorker bw = sender as BackgroundWorker;
if (f.TargetFolder != "" && f.TargetFolder != null)
{
createDirectory(f.TargetFolder);
}
else
{
f.TargetFolder = Path.GetDirectoryName(txf);
//createDirectory(f.TargetFolder);
}
string UploadPath = String.Format("{0}/{1}{2}", f.Host, f.TargetFolder == "" ? "" : f.TargetFolder + "/", Path.GetFileName(fn));
Now in txf for example i have C:test.txt
Then in f.TargetFolder i have: C:
Then in UploadPath i have: ftp://ftp.newsxpressmedia.com/C:/eula.1031.txt
But instead C: i need it to look like: ftp://ftp.newsxpressmedia.com/C/eula.1031.txt
And there are sub directories for example then: ftp://ftp.newsxpressmedia.com/C/Sub/Dir/eula.1031.txt
In the menuStrip1_ItemClicked event when i select a file for example test.txt already in this event i did a mess.
FileInfo fi = new FileInfo("temp.txt");
Determine the full path of the file just created.
DirectoryInfo di = fi.Directory;
Figure out what other entries are in that directory.
FileSystemInfo[] fsi = di.GetFileSystemInfos();
to display directoryinfo fullname in console
Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName);
Print the names of all the files and subdirectories of that directory.
foreach (FileSystemInfo info in fsi)
Console.WriteLine(info.Name);
Here
i'm building a Music PLayer and so i choose to use the library of Window Media Player:
Now i got stuck 'cos i wish show the song's name in a listBox and change songs in real time but i don't know how go on.
I store songs from a Folder and so when the Music Player run the songs from the Url choose.
I show you a code snippet :
private void PlaylistMidday(String folder, string extendsion)
{
string myPlaylist = "D:\\Music\\The_Chemical_Brothers-Do_It_Again-(US_CDM)-2007-SAW\\";
ListView musicList = new ListView();
WMPLib.IWMPPlaylist pl;
WMPLib.IWMPPlaylistArray plItems;
plItems = player1.playlistCollection.getByName(myPlaylist);
if (plItems.count == 0)
pl = player1.playlistCollection.newPlaylist(myPlaylist);
else
pl = plItems.Item(0);
DirectoryInfo dir = new DirectoryInfo(folder);
FileInfo[] files = dir.GetFiles(extendsion, SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
string musicFile01 = file.FullName;
string mName = file.Name;
ListViewItem item = new ListViewItem(mName);
musicList.Items.Add(item);
WMPLib.IWMPMedia m1 = player1.newMedia(musicFile01);
pl.appendItem(m1);
}
player1.currentPlaylist = pl;
player1.Ctlcontrols.play();
}
On Load i decide to play the songs of "myPLaylist" so i ask you do you know some way how to show the songs of my playlist in a listbox and when i click on the selected item i will change songs?
Thansk for your support.
Nice Regards
Instead of adding songs to playlist, you can add them to a List<string> as a return value. On load event, you just call the method that get list of media file paths in the folder, and then add them into a listbox.
To change the song being played, you just need to add SelectedValueChanged/SelectedItemChanged event, and in this event, get the file path that is currently selected in the listbox, then have WMP played it for you :)
private void Form1_Load(object sender, EventArgs e)
{
List<string> str = GetListOfFiles(#"D:\Music\Bee Gees - Their Greatest Hits - The Record");
listBox1.DataSource = str;
listBox1.DisplayMember = "str";
}
private List<string> GetListOfFiles(string Folder)
{
DirectoryInfo dir = new DirectoryInfo(Folder);
FileInfo[] files = dir.GetFiles("*.mp3", SearchOption.AllDirectories);
List<string> str = new List<string>();
foreach (FileInfo file in files)
{
str.Add(file.FullName);
}
return str;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string strSelected = listBox1.SelectedValue.ToString();
MessageBox.Show(strSelected); //Just demo, you can add code that have WMP played this file here
}
a quick solution. :). Not very good, but it works. Help this hope