By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,045 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

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

P: 6
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. }
  6.  
  7. //constructor code omitted
  8.  
  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.   }
  9.  
  10.   Public DirListService()
  11.   {
  12.     if (!Directory.Exists(path))
  13.     {
  14.       //code to log to event viewer that path doesn't exist
  15.     }
  16.   }
  17.  
  18.   protected override void OnStart(string[] args)
  19.   {
  20.     Thread t = new Thread(new ThreadStart(this.Start));
  21.     t.Start();
  22.   }
  23.  
  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
Share this Question
Share on Google+
7 Replies


RedSon
Expert 5K+
P: 5,000
Good question, I'm subscribing to this thread.
Apr 18 '07 #2

P: 12
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.
Ex.
H:\Documents (Doesn't Exist)
\\ComputerName\Documents\ (Does Exists)
Apr 18 '07 #3

P: 12
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

P: 6
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

P: 6
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

P: 12
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));
t.Start();
}

private void Start()
{
string path = "\\\\wks116\\Misc";
eventLog1.WriteEntry(path);

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




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

P: 6
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.