469,601 Members | 2,039 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,601 developers. It's quick & easy.

C# - APP: Troubles Getting a directory Listing of a Mapped Drive

I have a mapped share that I am trying to get a listing of all the files that it contains.

I use the following code to access the contents
String[] files = Directory.GetFiles(path);

I can then enumerate through that array to get my listing.

However I am trying to set my application up as a service. And now (after calling methods in a thread using the OnStart() method) it says the directory does not exist (I verified this with a Directory.Exists(path) call).

However I have a console app with the same calls but intitiated from the Main method and it works.

Example Console App:
Expand|Select|Wrap|Line Numbers
  1. Main (string[] args)
  2. {
  3.   DirectoryLister dirlist = new DirectoryLister();
  4.   dirlist.listfiles();
  5. }
  7. //constructor code omitted
  9. private listfiles()
  10. {
  11.    String[] files = Directory.GetFiles(path);
  12.    //code to write to console the list of files.
  13. }
Example Service App:
Expand|Select|Wrap|Line Numbers
  1. public class DirListService() : System.ServiceProcess.ServiceBase
  2. {
  3.   static void Main()
  4.   {
  5.     System.ServiceProcess.ServiceBase[] ServicesToRun;
  6.     ServicesToRun = new System.ServiceProcess.ServiceBase[] { new DirListService() };
  7.     System.ServiceProcess.ServiceBase.Run(ServicesToRun);
  8.   }
  10.   Public DirListService()
  11.   {
  12.     if (!Directory.Exists(path))
  13.     {
  14.       //code to log to event viewer that path doesn't exist
  15.     }
  16.   }
  18.   protected override void OnStart(string[] args)
  19.   {
  20.     Thread t = new Thread(new ThreadStart(this.Start));
  21.     t.Start();
  22.   }
  24.   private void Start()
  25.   {
  26.      //service loop which calls for a directory listing i.e
  27.      String[] files =  Directory.getFiles();
  28.      //code to process the list of files retrieved
  29.   }
  30. }

I've done a lot of testing around this and can't seem to find a solution or work around. I'm using Visual Studio .NET 2003 and testing on a Windows Server 2003 box. I've made sure that the mapped drive can be seen via other methods and of course the console application can see it wheras the service cannot. Another telling symptom is that I can break the console app by not making the listfiles() method call from the constructor and not the main method.

so the bottom line is same path, same test machine, two different results. Why?
Apr 18 '07 #1
7 5155
5,000 Expert 4TB
Good question, I'm subscribing to this thread.
Apr 18 '07 #2
The problem is that the drive is mapped to your username. Unless you're starting the service under your user account that mapped drive doesn't exist. Generally the service starts under the Local System Account. You have two options here. First is to start the service under your user account. Second is to use the full UNC path rather than the mapped Drive Path.
H:\Documents (Doesn't Exist)
\\ComputerName\Documents\ (Does Exists)
Apr 18 '07 #3
Also, If you're going to use the system account; make sure the System account has the approriate NTFS permissions and Permissions to the Share.
Apr 18 '07 #4
Thas a very good suggestion. I will check the path to see if just using the UNC path will work. It hasn't previously but that was many cycles ago.

However how does that explain the console app which experiences the same problem under the same conditions? I don't believe that it would run as a local system account since it is started by the user.

Finally as regards to NTFS permissions I have made sure that this share is as open as possible.
Apr 18 '07 #5
As I feared the UNC path direction didn't work.

Any other ideas? I think the key here is the fact that from the main method it works fine versus not when it is called via an class internal call.
Apr 19 '07 #6
Excuse my C#... I usually program in VB.
I added a path string and event log, and installed the service.
It worked perfectly. The only thing I can think of that would be failing is the path you're using. If I were you I would add a timer to the service and move the thread start to the timer elapsed event. Then I would compile and install a debug version and attach to the process while it's running on the server. This way you could see the path variable as it's passed.

protected override void OnStart(string[] args)
Thread t = new Thread(new ThreadStart(this.Start));

private void Start()
string path = "\\\\wks116\\Misc";

//service loop which calls for a directory listing i.e
String[] files = System.IO.Directory.GetFiles(path);
int i = files.GetLength(0);
while (i > 0)
eventLog1.WriteEntry(files[i - 1]);

As I feared the UNC path direction didn't work.

Any other ideas? I think the key here is the fact that from the main method it works fine versus not when it is called via an class internal call.
Apr 19 '07 #7
Thank you for your reply.

In the original version both console and service a timer was firing the directory search and handling the processing. I just didn't think that detail was important to add to the information provided.

However you were correct in your original assessment. I went through it again and checked my paths and user account logins and all seemed to be correct until I thought that I would change the user account the service uses to match a local administrative account on the source server (the destination server is on a domain whereas the source server is not).

Now the service works. By the way I am also using a UNC path, thank you for the suggestion!

so now both the service and the console app work just fine. Situation Resolved.
Apr 20 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Hal Vaughan | last post: by
2 posts views Thread by paii, Ron | last post: by
5 posts views Thread by Michael C | last post: by
4 posts views Thread by =?Utf-8?B?c2hhZG93?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.