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.

4
  • post some codes where you are creating the .txt file Commented Apr 6, 2013 at 10:21
  • Where is that file, on the user's computer, or the server? From asp.net you can only access the server! Commented Apr 6, 2013 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 Commented Apr 6, 2013 at 10:34
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?. Commented Apr 6, 2013 at 10:48

3 Answers 3

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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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!
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?

Comments

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();

6 Comments

What if other computer doesn't have that path/access permissions?
he will have to share the folder for the user who is running the IIS.
Then why can't you use Server.MapPath? instead of setting manually
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.
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..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.