0

///Save images into Images folder

  string strFileName = Path.GetFileName(uploadImage.PostedFile.FileName);

         string strPath = "~/Files/";
             uploadImage.SaveAs(Server.MapPath(strPath + strFileName));

        /////ReadSavedFile/////
       StreamReader srFileReader = new StreamReader(strPath + strFileName);

/// strPath.... Is Showing me Error i can give the whole Path But i Need to give the Path Of My Folder In Solution Explorel

DAUD WANI
  • 41
  • 5
  • this is likely duplicate of http://stackoverflow.com/questions/10951599/getting-current-directory-in-net-web-application, but it is not clear why using `MapPath` on `~/Files/file.name` did not work for you. Please clarify (and make sure sample is correct - code shows different path for writing and reading). – Alexei Levenkov Dec 08 '16 at 06:26
  • First i am uploading a file to folder then i am reading that file code is correct – DAUD WANI Dec 08 '16 at 06:33
  • and i am getting error when it reaches at the reader – DAUD WANI Dec 08 '16 at 06:45
  • One more time - code shows that you write to one location and read from completely different one. Since you are claiming code is exactly the way you want it to be I don't see why you expect StreamReader to read saved file. I hope you'll figure out why you want to use different paths in these two cases... – Alexei Levenkov Dec 08 '16 at 06:46
  • (I suspect this problem is just "typographical error" and you can't see missing `Server.MapPath` in the second call for some reason... following DRY principles helps - i.e. in this case you'd use `var path = Server.MapPath(strPath + strFileName)` and use it in both calls... ) – Alexei Levenkov Dec 08 '16 at 06:50
  • yes.. thanks it is working this way but it is actuallly taking which path can u tell me... – DAUD WANI Dec 08 '16 at 07:00

3 Answers3

0

string dir =System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location);

string file = dir + @"\TestDir\TestFile.txt";

string fullDirectory = directory.FullName; string fullFile = file.FullName;

StreamReader sr = new StreamReader(@fullDirectory + fullFile);

  • I am just trying to get the file from my folder in my solution explorer and to reader i wan to give that path.. – DAUD WANI Dec 08 '16 at 06:27
0

You can use:

string strPath = "../../Files/";
StreamReader srFileReader = new StreamReader(strPath + strFileName);

or copy the Files folder in Debug/Release folder and use:

string strPath = "Files/";
StreamReader srFileReader = new StreamReader(strPath + strFileName);
daniel
  • 132
  • 7
  • Could not find a part of the path 'C:\Program Files (x86)\Files\Book2.csv'. this is the error it is showing me when i use your First Suggested way – DAUD WANI Dec 08 '16 at 06:40
-1

try using the literal operator(@) around your path

string strPath = @"~/Files/";

Darthchai
  • 757
  • 8
  • 17
  • "Try xxxxxx" is very poor explanation for an answer. If you have reason why you believe particular solution will help - explain that in the post. (In this particular case I see no way you can explain this pointless suggestion... but give it a try) – Alexei Levenkov Dec 08 '16 at 06:22