Click here to Skip to main content
12,295,976 members (53,109 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET WebForms

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.

Posted 22-Feb-13 23:59pm
CShubham 23-Feb-13 6:07am
Make confirm its a web application or window application!!!!!
tcpvivek 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
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">
        <allow users="*" />

This will allow the access to this folder at runtime.
tcpvivek 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"
"C:\Users\\Documents\Visual Studio 2012\Projects\Property utility\WindowsFormsApplication3"

I have my code files placed inside

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";
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Did you tried using the Server.MapPath?
you can use Server.MapPath("your path")
Rate this: bad
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100