473,854 Members | 1,555 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.GetFi les(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.Exist s(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 5503
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(thi s.Start));
t.Start();
}

private void Start()
{
string path = "\\\\wks116\\Mi sc";
eventLog1.Write Entry(path);

//service loop which calls for a directory listing i.e
String[] files = System.IO.Direc tory.GetFiles(p ath);
int i = files.GetLength (0);
eventLog1.Write Entry(i.ToStrin g());
while (i > 0)
{
eventLog1.Write Entry(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
19408
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 OpenOffice. I know in *nix I can start by using "/" as the file name and doing a recursive listing of the file system from there. (Yes -- I know about permissions to read directories -- that's another issue I've already solved.) How do I find the...
2
2689
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 backend using UNC path including a DFS root. Currently I link through a mapped drive letter.
5
2491
by: Michael C | last post by:
....on a remote machine? Thanks, Michael C.
5
4479
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 seperate files server. we have mapped the fodler to a different folder and need to allow the EXE to process on the mapped drive. When I trigger the EXE via web service the EXE get the permission of the launching user (mean ASP.NET user) resulting a...
3
6820
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 answer... Thanks in advance!
5
1585
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 error of "Can not file specified module". Mapped drive is added to path. Thanks, Marc
4
3647
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 http://localhost:1245/. Is this right or not? ALso the ASP.NET web site administration tool shows error in the Security tab as: There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient...
1
1350
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 is not working, if I run it locally, it runs normal. It s related to some permission issues I think,
0
1436
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 a reference to a class library, to a remote server where I don't have admin rights to? This is basically what I did. 1. Run sn.exe and add strong names each for both console app and class library projects
0
9901
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11025
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10682
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10371
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9513
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4562
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 we have to send another system
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3187
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.