Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
[no name] 23-Feb-13 6:07am    
Make confirm its a web application or window application!!!!!
VivekSharma0925 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.

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:
XML
<location path="MyFiles">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>


This will allow the access to this folder at runtime.
 
Share this answer
 
v3
Comments
VivekSharma0925 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\<user>\Documents\Visual Studio 2012\Projects\Property utility\WindowsFormsApplication3\bin\Debug"

What I want as result is:
"C:\Users\<user>\Documents\Visual Studio 2012\Projects\Property utility"
OR
"C:\Users\<user>\Documents\Visual Studio 2012\Projects\Property utility\WindowsFormsApplication3"

I have my code files placed inside
...\\WindowsFormsApplication3\CodeFiles\abc.txt

Thanks
Raul Iloc 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";
Did you tried using the Server.MapPath?
you can use Server.MapPath("your path")
 
Share this answer
 
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.

C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900