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
- Main (string[] args)
- {
- DirectoryLister dirlist = new DirectoryLister();
- dirlist.listfiles();
- }
- //constructor code omitted
- private listfiles()
- {
- String[] files = Directory.GetFiles(path);
- //code to write to console the list of files.
- }
Expand|Select|Wrap|Line Numbers
- public class DirListService() : System.ServiceProcess.ServiceBase
- {
- static void Main()
- {
- System.ServiceProcess.ServiceBase[] ServicesToRun;
- ServicesToRun = new System.ServiceProcess.ServiceBase[] { new DirListService() };
- System.ServiceProcess.ServiceBase.Run(ServicesToRun);
- }
- Public DirListService()
- {
- if (!Directory.Exists(path))
- {
- //code to log to event viewer that path doesn't exist
- }
- }
- protected override void OnStart(string[] args)
- {
- Thread t = new Thread(new ThreadStart(this.Start));
- t.Start();
- }
- private void Start()
- {
- //service loop which calls for a directory listing i.e
- String[] files = Directory.getFiles();
- //code to process the list of files retrieved
- }
- }
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?