Solved

How to specify a relative path

Posted on 2011-02-18
8
1,263 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
  • 4
  • 3
8 Comments
 
LVL 7

Accepted Solution

by:
JosephEricDavis earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Dovberman
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

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

Assisted Solution

by:JosephEricDavis
JosephEricDavis earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now