?
Solved

How to specify a relative path

Posted on 2011-02-18
8
Medium Priority
?
1,277 Views
Last Modified: 2012-06-22
The following code works on the local machine but not on the server:

strSourcePath = "C:\\Develop\\Apps\\StockPro\\Data\\";
                       
        // Fill Local files listbox
        lstSourceFiles.Items.Clear();
        string strFileName = "";
        int intFileCount = 0;
        foreach (string FileName in Directory.GetFiles(strSourcePath, "*.txt", SearchOption.TopDirectoryOnly))
        {
            intFileCount += 1;
            strFileName = FileName;
            lstSourceFiles.Items.Add(strFileName);
        }

A file not found error is returned.

I know that I need to specify a relative path to "C:\\Develop\\Apps\\StockPro\\Data\\"

How is this done?

Thanks,
0
Comment
Question by:Dovberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 7

Accepted Solution

by:
JosephEricDavis earned 1000 total points
ID: 34926094
Give this a look see...

http://msdn.microsoft.com/en-us/library/ms178116.aspx

Basically if you begin a url in an aspx page with "~/" It symbolizes the root of a directory.

So in your case you might use something like this...

strSourcePath = "~/Data/";

But I'm not sure what the root of your website is from what you have provided.
0
 
LVL 7

Expert Comment

by:JosephEricDavis
ID: 34926117
But you might actually need to use the MapPath method to accomplish what you're after...

http://dotnetperls.com/mappath

So in this case it would be something like this...

strSourcePath = MapPath("~/Data/");
0
 

Author Comment

by:Dovberman
ID: 34926987
string strMapPath = Server.MapPath("");

strMapPath = "C:\\Develop\\Apps\\StockPro\\Admin"

// Local directory
strSourcePath = "C:\\Develop\\Apps\\StockPro\\Data\\";

strSourcePath = MapPath("~/Data/");

returns
 "C:/Develop/Apps/StockPro/Data/";

This works on the local machine.
I will now try it on the server.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Dovberman
ID: 34927161
This works on the local machine but not on the server.

strSourcePath = MapPath("~/Data/");

returns
 "C:/Develop/Apps/StockPro/Data/"; // This is where the .txt file is.



There is no error message.

intFileCount  returns zero.

   foreach (string FileName in Directory.GetFiles(strSourcePath, "*.txt", SearchOption.TopDirectoryOnly))
        {
            intFileCount += 1;
            strFileName = FileName;
            lstSourceFiles.Items.Add(strFileName);
        }

intFileCount  returns zero.
Yet there is a .txt file in the directory.

Suggestions?

Thanks,

0
 
LVL 12

Expert Comment

by:Hairbrush
ID: 34941390
Does it return anything if you change the wildcard to *.*?
0
 
LVL 7

Assisted Solution

by:JosephEricDavis
JosephEricDavis earned 1000 total points
ID: 34942814
Do you have access to look at the directory structure of the server to see what is actually going on?
0
 

Author Comment

by:Dovberman
ID: 34943782
The mis-understanding was mine.

I did not realize that mappath refers to directories on the server.

I was attempting to access directories on my local machine.
0
 

Author Closing Comment

by:Dovberman
ID: 34943795
Thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question