tags:

views:

1009

answers:

6

Hi, I'm trying to create a text file in my winforoms application. It is working fine but it is creating in its default location (like in bin folder). But i want to save that created text file in my userfiles folder. How can i do it?

this is my trail

FileInfo fileusername = new FileInfo("userinfo.txt");
StreamWriter namewriter = fileusername.CreateText();
namewriter.Write(txtUsername.Text);
namewriter.Close();

Thanks in advance

A: 

Just add the path to the filename, like FileInfo fileusername = new FileInfo(@"c:\Users\MyUser\Documents\userinfo.txt");

k0ni
i want to create in my application folder only (dynamicall)
Nagu
Please stay clear from my documents folder as an application. "My Documents" is called like that because *I* create things there, not some random application. There are enough programs that get this wrong already.
Joey
+5  A: 

You can use Environment.GetFolderPath to get the path to the user data folder:

string fileName = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
    "userinfo.txt");

Look into the documentation of the Environment.SpecialFolder enum in order to find the folder that best suits your needs.

Fredrik Mörk
I got it thank you Directory.GetCurrentDirectory()
Nagu
A: 

Hello, use this template:

String path = Environment.GetFolderPath( Environment.SpecialFolder.Personal );

OR

String path = Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments );
Manushin Igor
Please stay clear from my documents folder as an application. "My Documents" is called like that because *I* create things there, not some random application. There are enough programs that get this wrong already.
Joey
thank you for your quick response. But I'm sorry i was not frame my question correctly.. i want to create a file in my application forlder only (like c:\\myapp\\usercreatedfiles\ ) here i want to create
Nagu
@Nagu: don't do that: this will require the user to have full permissions to the folder in which the application is installed, which is typically not a good thing.
Fredrik Mörk
oh actually i tried with Directory.GetCurrentDirectory() it is working fine for me. Is it not good? then wat is the alternate and good method?
Nagu
Nagu: The user's profile for per-user data, the "All users" profile for global data. But careful with stuff that's globally writable for every user.
Joey
A: 

You can use Environment.SpecialFolder to create a filepath that points to my documents:

string filePath = System.IO.Path.Combine(Environment.SpecialFolder.ApplicationData.ToString(), "userinfo.txt");
Christian
I misread the question and fixed it already
Christian
+1  A: 

You can use the Evironment.GetFolderPath() function together witrh the Environment.SpecialFolder enumeratrion.

use:

String filePath = Path.Combine(
    Evironment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
    "userinfo.txt");

to create the file name in the current users My Documents folder.

Frank Bollack
A: 

From the looks of it you simply want to create your text file in a custom directory. My advice would be to create a constant for the path (perhaps saving it in the app.config file).

<appSettings>
    <add key="UserInfo" value="/Settings/UserInfo.txt" /> 
</appSettings>

Use a relative path to your exe so regardless of where you install your application the settings will always be saved to "PathToExe/Settings/UserInfo.txt".

Then you would do something like:

string UserInfoPath = ConfigurationManager.AppSettings["UserInfo"];
if (String.IsNullOrEmpty(UserInfoPath))
{
    // perhaps use a default value or raise an exception
}

FileInfo fileusername = new FileInfo(Path.Combine(Application.StartUpPath, UserInfoPath));
StreamWriter namewriter = fileusername.CreateText();
namewriter.Write(txtUsername.Text);
namewriter.Close();

However, you may come across a permissions issues using this approach, it is usually a better idea to store things like this either in the Registry or in the AppData directory.

James
oh thank you verymuch
Nagu