[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1294
  • Last Modified:

How to specify a relative path

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
Dovberman
Asked:
Dovberman
  • 4
  • 3
2 Solutions
 
JosephEricDavisCommented:
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
 
JosephEricDavisCommented:
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
 
DovbermanAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DovbermanAuthor Commented:
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
 
HairbrushCommented:
Does it return anything if you change the wildcard to *.*?
0
 
JosephEricDavisCommented:
Do you have access to look at the directory structure of the server to see what is actually going on?
0
 
DovbermanAuthor Commented:
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
 
DovbermanAuthor Commented:
Thank you
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now