Solved

How to specify a relative path

Posted on 2011-02-18
8
1,271 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 250 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC 2 40
Save ms data to server side. 19 55
How come this regular expression allows "e"? 14 45
Iteration Help (Asp.net VB) 5 24
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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