Join 150,071 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,809 people online right now. Registration is fast and FREE... Join Now!
media.PlaylistDirectory = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) + "\\My Playlists\\");
Is returning C:\My Playlists, meaning that the only thing being returned is the hard coded "My Playlists". Is there a way to use this in an ASP.Net application, or am I forced to hard code it?
I know it works in a Windows Application, is there a way to use this in ASP.Net?
Well I guess it's back to the drawing board. Your idea would probably work if this wasn't going to be a public web application where I would need the path to the user viewing the site's My Music folder.
EDIT:
What I'm trying to get is the path to the viewers My Music folder, not of the server, does that make sense?
This post has been edited by PsychoCoder: 9 Feb, 2008 - 01:26 PM
Hmm..does it work when you are testing it on your local machine, but not when you test it remotely from the server?
Or does it not work either way?
I can get it to work, just fine on my local machine. But I don't have a server available that I can set the project up on to test it remotely, so I need a little more info from you.
I am running it on my dev server (which is the box I work on as well) and it's not even working then. When I tried using SHGetFolderPath it gave me the path for the ASPNET account's My Music folder. The only way I have been able to get it to work is to hard code the path to my My Music folder, which doesn't do me any good as that way only I can use it.
I never thought of this snag when I started this project lol, but there has to be some way that in code I can find the viewers My Music folder from the web server, but I've run into brick wall after brick wall. If this were a Windows Application this wouldn't even be an issue because I could just use Environment.SpecialFolders.MyMusic, but thats not available from a web application.
I have a test project set-up and it works just fine.
Here is the test project code I am using:
CODE
public partial class _Default : System.Web.UI.Page { string ROOTDIR = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
private void PopulateLibrary() {
TreeNode artist; TreeNode album; TreeNode song; //objects to get artists DirectoryInfo dir = new DirectoryInfo(ROOTDIR);
// Add all the sub-directories foreach (DirectoryInfo dirInfo in dir.GetDirectories("*.*")) { // Add the artist node artist = new TreeNode(dirInfo.FullName.Substring(dirInfo.FullName.LastIndexOf(Path.DirectorySeparatorChar) + 1)); artist.SelectAction = TreeNodeSelectAction.Select;
//objects to get the albums DirectoryInfo[] subDirs = dirInfo.GetDirectories();
foreach (DirectoryInfo sd in subDirs) { album = new TreeNode(sd.Name); album.SelectAction = TreeNodeSelectAction.Select;
//get the songs FileInfo[] files = sd.GetFiles();
foreach (FileInfo fi in files) { song = new TreeNode(fi.Name); //song.ShowCheckBox = true; //song.SelectAction = TreeNodeSelectAction.Expand; album.ChildNodes.Add(song); }
artist.ChildNodes.Add(album); } // Add the nodes MediaLibrary.Nodes.Add(artist); MediaLibrary.CollapseAll(); } }
//check if this is a song node, if it is then don't do anything if (nodePath.IndexOf("/") == nodePath.LastIndexOf("/")) { //if the index is -1 then this is a parent node //otherwise expand the parent node and then the childnode if (nodePath.IndexOf("/") != -1) { string parentNode = nodePath.Substring(0, nodePath.IndexOf("/")); this.MediaLibrary.CollapseAll(); this.MediaLibrary.FindNode(parentNode).Expand(); this.MediaLibrary.FindNode(nodePath).Expand(); } else { this.MediaLibrary.CollapseAll(); this.MediaLibrary.FindNode(nodePath).Expand(); } } }
Thats weird, I tried that (from my first post) and it was returning an empty string. Let me take a look at my code again, but that looks almost identical to how I was doing it but I was assigning it to a variable first.
And all I'm getting is Folder Path: with no path after it. I started trying to write it out because I was getting an error that I wasn't providing a path, and even now it's not showing a path at all (I even tried adding .ToString() to no avail.
It's essentially the same deal, but have you tried using the registry? Just briefly searching through the registry I found the key: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders.
I will note this is in Vista, so it could have different entries, so it's probably advisable you do a search using regedit on an XP machine.
Are you using IIS or the built in server in VS? Maybe thats the cause for the difference?
Ok, I finally figured out what is going on. Yes, my first project was using an instantiated version of IIS. I had forgotten that I set up the project that way.
So I set up a new HTTP project using IIS. And I did get the same result as you, no path is returned.
I also came up with the idea that RodgerB suggested, but it will only end up giving you the same result.
Here is why it will not work. The issue is that when using IIS, you are using the ASP.NET worker process user account to access the page in IIS. The worker process does not have access to any local user accounts especially on a remote computer, which is what you need in order to get the local users path to the My Music folder.
So the bottom line is, it will never work, you won't have the necessary permissions to access the information you need.