473,729 Members | 2,355 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 5497
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
19399
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
2685
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
2484
by: Michael C | last post by:
....on a remote machine? Thanks, Michael C.
5
4476
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
6813
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
1574
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
3643
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
1347
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
1430
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
8917
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
9426
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
9281
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...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
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.