Welcome to Dream.In.Code
Become an Expert!

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!




Get path to viewers My Music folder in ASP.Net

2 Pages V  1 2 >  
Reply to this topicStart new topic

Get path to viewers My Music folder in ASP.Net

PsychoCoder
9 Feb, 2008 - 12:09 PM
Post #1

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
In my web application Im trying to get the path to the My Music folder by using

CODE

Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)


And it's retuning nothing, I mean this line

CODE

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?
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Get Path To Viewers My Music Folder In ASP.Net
9 Feb, 2008 - 12:36 PM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
It's because ASPX is running under its own user account. You could do three things (that I know of):
  1. Run ASPX under a specific user account (bad idea)
  2. Write another program which you run by opening a process with user credentials and getting that to return the directory, OR
  3. Write a service/always running program that your ASP code can communicate with.
  4. Use the SHGetFolderPath API

From MSDN:
CODE

HRESULT SHGetFolderPath(      
    HWND hwndOwner,
    int nFolder,
    HANDLE hToken,
    DWORD dwFlags,
    LPTSTR pszPath
);

You can look it up yourself.

But you will need to call LogonUser to get the value for hToken.

Hope that helps.

I just noticed, I can't count.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Get Path To Viewers My Music Folder In ASP.Net
9 Feb, 2008 - 01:03 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Get Path To Viewers My Music Folder In ASP.Net
9 Feb, 2008 - 06:22 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Get Path To Viewers My Music Folder In ASP.Net
9 Feb, 2008 - 06:28 PM
Post #5

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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.

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Get Path To Viewers My Music Folder In ASP.Net
9 Feb, 2008 - 06:38 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
What do you mean it is not available?

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();
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        PopulateLibrary();
        
    }
    protected void MediaLibrary_SelectedNodeChanged(object sender, EventArgs e)
    {        
        string nodePath = this.MediaLibrary.SelectedNode.ValuePath;
        
        //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();
            }
        }
    }

}

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Get Path To Viewers My Music Folder In ASP.Net
9 Feb, 2008 - 06:42 PM
Post #7

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Get Path To Viewers My Music Folder In ASP.Net
9 Feb, 2008 - 06:47 PM
Post #8

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Well now Im really confused as to why it's working for you and not me. I am using it like:


CODE

protected void Page_Load(object sender, EventArgs e)
{
    if (!(IsPostBack))
    {
        string ROOTDIR = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
        HttpContext.Current.Response.Write(string.Format("Folder Path: {0}",ROOTDIR));
    }
}


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.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Get Path To Viewers My Music Folder In ASP.Net
9 Feb, 2008 - 06:55 PM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
That is very odd, I just used the code you posted and it still worked fine in my test project.

I get returned Folder Path: C:\Documents and Settings\Jason\My Documents\My Music
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Get Path To Viewers My Music Folder In ASP.Net
9 Feb, 2008 - 07:00 PM
Post #10

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Are you using IIS or the built in server in VS? Maybe thats the cause for the difference?

The best Ive got is with this code (it at least returns a partial path)

CODE

protected void Page_Load(object sender, EventArgs e)
    {
        if (!(IsPostBack))
        {
            string ROOTDIR = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
            DirectoryInfo dir = new DirectoryInfo(ROOTDIR + "\\My Playlists\\");
            HttpContext.Current.Response.Write(string.Format("Folder Path: {0}",dir.FullName));
        }
    }


With gives me Folder Path: C:\My Playlists\, which of course is wrong blink.gif
User is offlineProfile CardPM
+Quote Post

RodgerB
RE: Get Path To Viewers My Music Folder In ASP.Net
9 Feb, 2008 - 07:18 PM
Post #11

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
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.

Hope that helps smile.gif.
User is online!Profile CardPM
+Quote Post

Jayman
RE: Get Path To Viewers My Music Folder In ASP.Net
9 Feb, 2008 - 08:08 PM
Post #12

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
QUOTE(PsychoCoder @ 9 Feb, 2008 - 07:00 PM) *

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.

Wish I had some other suggestions. sad.gif
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:59PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month