1

I am trying to design a web form in ASP.NET using Visual Studio Express 2012.

I need to take data from some text boxes, and write it to a Text file.

This is a very simple program, but I am having trouble setting the file path.

This application will be sent to someone else, and they have to run the project from their computer.

The file I want is called Database.txt and it is found in D:\Project\bin\Database.txt.

So if he pastes this folder in his Desktop, it becomes C:\Users\Desktop\Project\bin\Database.txt.

I am having trouble setting a dynamic path that can find this file regardless of where the project folder is.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
  • post some codes where you are creating the .txt file – polin Apr 06 '13 at 10:21
  • Where is that file, on the user's computer, or the server? From asp.net you can only access the server! – Hans Kesting Apr 06 '13 at 10:22
  • I prefer avoiding use of absolute path while writing into files.. You can use `Server.MapPath` method to access your application directory – Raghuveer Apr 06 '13 at 10:34
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Apr 06 '13 at 10:48

3 Answers3

2

Use this code:

public void WriteToFile(String text)
{
    string logFileName = Server.MapPath("~/bin/DataBase.txt");
    using (StreamWriter writer = File.AppendText(logFileName))
    {
        writer.WriteLine(text);
        writer.Flush();
        writer.Close();
    }
}
Ken Clark
  • 2,320
  • 13
  • 15
  • Thank you for the quick response! But this is in C sadly, My project is based off VB. Or is there some way to integrate the two? Thanks in advance again! – Sainath Krishnan Apr 06 '13 at 20:04
1

use Server.MapPath("/") to get physica path of ur virtual directory

see this. post for server.mappath Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?

Community
  • 1
  • 1
Amit Gaikwad
  • 913
  • 1
  • 8
  • 14
0

What you can do is put the path in web.config file and set the values as per requirement.

Here is some sample code that should help you

This goes into your web.config.

<configuration>
    <appSettings>
        <add key="myFilePath" value="C:\\whatever\\Data\\"/>
    </appSettings>
</configuration>

And this is how you read it:

path = System.Web.Configuration.WebConfigurationManager.AppSettings["myFilePath"].ToString();
शेखर
  • 16,910
  • 12
  • 52
  • 105
  • What if other computer doesn't have that path/access permissions? – Raghuveer Apr 06 '13 at 10:31
  • he will have to share the folder for the user who is running the IIS. – शेखर Apr 06 '13 at 10:32
  • Then why can't you use `Server.MapPath`? instead of setting manually – Raghuveer Apr 06 '13 at 10:34
  • you can use that also but that's a path where all you files(code files dll etc) will be kept. And you should not keep your file there. – शेखर Apr 06 '13 at 10:43
  • Why? We can create a folder with name `log` or `temp` in root of the application folder. We can use that folder right? and that will not effect other files(code or DLL).. This way all our application files reside only in one folder not on different folders.. This way we can reduces the maintenance of file system also for our website.. – Raghuveer Apr 06 '13 at 10:57
  • I just wanted to know.. Is there any specific reason for using out side folders for this kind of requirement? – Raghuveer Apr 06 '13 at 11:01