Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NETWebFormsc#.
Hi,
 
I have to create a string from a text file using c#.
This part for anyone is typically very easy.
 
The problem lies here:
I have my Project Folder as: Property_Utility
Inside this folder, I have WindowsFormsApplication3 Folder, 1 .suo file and 1 .sln solution file.
 
In WindowsFormsApplication3 folder, I have my design and cs files.
 
Now, I have placed my .txt files in this folder.
 
Environment.CurrentDirectory read the location "~\WindowsFormsApplication3\bin\debug"
 
So I can read the file, from debug folder but not from WindowsFormsApplication3 folder.
I cannot manually give a particular absolute path as the project is intended to run on multiple machines.
 
Please give suggestions how to read WindowsFormsApplication3 folder location directly.
 
Thanks,
Vivek
Posted 22-Feb-13 23:59pm
Comments
CShubham at 23-Feb-13 6:07am
   
Make confirm its a web application or window application!!!!!
tcpvivek at 23-Feb-13 14:17pm
   
My bad. ITs a windows application form. Although it doesn't matter much. Since beginning I am coding on websites and this is my first form. I can't see much difference except I cant use AJAX in this. My apologies for the confusion again. I will keep track from next time.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Solved this Myself.
 
Since starting I was able to get the location of debug folder.
I figured out a way to go back 2 folder up.
 
Here's how.
 
string codefolderpath = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString()
;
 
Thanks all for help.
 
In this jest... I studied about Site.Map and Directory and couple more things.
Thanks a lot.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

1.In your "Web Site Project" you should add a folder named "MyFiles" for example, by using Visual Studio;
2.Move your .txt file into this folder;
3.In Visual Studio select this file to be part of the project, then select the file and in the Property window, set up the "Build Action" property with value "Content" and the property "Copy to Output" should be "Not Copy";
3.In Web.Config add the next lines inside the "configuration" section:
<location path="MyFiles">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
 
This will allow the access to this folder at runtime.
  Permalink  
v3
Comments
tcpvivek at 23-Feb-13 13:44pm
   
This seems like a promising solution, I have done as told by you but I need to read the location into a string like String rootpath = Environment.CurrentDirectory; But above command will give me as result "C:\Users\\Documents\Visual Studio 2012\Projects\Property utility\WindowsFormsApplication3\bin\Debug" What I want as result is: "C:\Users\\Documents\Visual Studio 2012\Projects\Property utility" OR "C:\Users\\Documents\Visual Studio 2012\Projects\Property utility\WindowsFormsApplication3" I have my code files placed inside ...\\WindowsFormsApplication3\CodeFiles\abc.txt Thanks
Raul Iloc at 23-Feb-13 14:04pm
   
To access the file from the "MyFiles" folder (folder that will be in the IIS deployment location of your web site), in your source code you simple use a relative path like next code: string fileName = "~/MyFiles/FileName.txt";
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Did you tried using the Server.MapPath?
you can use Server.MapPath("your path")
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 446
1 Sergey Alexandrovich Kryukov 360
2 Shai Vashdi 318
3 OriginalGriff 265
4 Peter Leow 171
0 Sergey Alexandrovich Kryukov 9,109
1 OriginalGriff 5,290
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 23 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid