Solved

How to specify a relative path

Posted on 2011-02-18
8
1,272 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Industry Leaders: 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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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