473,498 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

6 New Member
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
7 5463
RedSon
5,000 Recognized Expert Expert
Good question, I'm subscribing to this thread.
Apr 18 '07 #2
stlarmon
12 New Member
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
stlarmon
12 New Member
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
epikto
6 New Member
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
epikto
6 New Member
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
stlarmon
12 New Member
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
epikto
6 New Member
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

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

Similar topics

3
19384
by: Hal Vaughan | last post by:
I've seen install programs that search a hard drive for previous instances of a program, or to find installs of other needed programs. I need to search a hard drive for any installations of...
2
2664
by: paii, Ron | last post by:
I am converting to Active Directory and Distributed File System. All my server storage will be mapped though the DFS root. Does anyone have experience with linking an Access front end to the...
5
2472
by: Michael C | last post by:
....on a remote machine? Thanks, Michael C.
5
4444
by: Nirosh | last post by:
Hi All, Can any one suggest me a best way to do this .. I have a thrid party tool "EXE" that we need to use with our web service to manipulate some complex XML files, which reside in a...
3
6794
by: KSC | last post by:
Hello, Is there a way to programmatically determine if a directory is shared and if so, what the sharename is? It seems a simple question, but I have been searching and not found the...
5
1564
by: Marc | last post by:
I am trying to run a web service that has pre-compiled dll's that reference dll's that are on a mapped drive. The web service can not load because it does not see that mapped drive and returns an...
4
3621
by: =?Utf-8?B?c2hhZG93?= | last post by:
I have a asp.net 2.0 web application created on a mapped web server drive, a very simple one with only one default page which is blank. But when I run it inside VS, the URL is...
1
1335
by: Sylvie | last post by:
Hello, I have recently developed a windows application using VS.Net 2005 & Devexpress Commponents. When I try to run application on the client using a mapped drive on my SBS Server 2003, app...
0
1417
by: nikib | last post by:
Hi, I am new in the forum and new in .Net. I've tried to search for answers on web to no avail. Can anybody help me with this problem? Is it possible to install a console app, that also has...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.