I want to save my text file in a F drive but this file is written to a default folder of program . How to save it by guiding a path
string[] contents = new string[2];
contents[0] = "Name: " + textBox1.Text;
contents[1] = "age: " + textBox2.Text;
string path = #"F:\\"; // path to file
System.IO.File.WriteAllLines(textBox1.Text + ".txt", contents);
It would be a good idea to actually use your path variable:
string path = System.IO.Path.Combine(#"F:\", textBox1.Text + ".txt");
System.IO.File.WriteAllLines(path, contents);
Because you defining a path,but you don't use it.
string path = #"F:\" + textBox1.Text + ".txt";
File.WriteAllLines(path, contents);
As an alternative, you can use File.Move after you created it like;
File.WriteAllLines(textBox1.Text + ".txt", contents);
File.Move(Directory.GetCurrentDirectory() + textBox1.Text + ".txt",
path + textBox1.Text + ".txt");
Related
I am trying to increment the filename (e.g., "file1.csv", "file2.csv", etc.), each time a new file is generated. I followed this thread Increment the file name if the file already exists in c# but the solution is not useful for my case. What I want to do is check if the file exists in the first place and if it does write in it. If it doesn't create one and write. The problem is that if the file exists but it's from another user, I want the system to increment the file number and not write to the same file just because it exists. What I have so far:
public void saveFile()
{
int count = 0;
string title = "TimeStamp,Name,Trial,Time_spent-dist,Time_spent_tar\n";
string output = System.DateTime.Now.ToString("mm_ss_ffff") + "," +
currentScene.name.ToString() + "," +
trialNum.ToString() + "," +
timerDistractor.ToString() + "," +
timerTarget.ToString();
string fname = "User_" + count + ".csv";
string path = Path.Combine(Application.persistentDataPath, fname);
if (File.Exists(path))
{
File.AppendAllText(path, "\n" + output);
}
else
{
StreamWriter writer = new StreamWriter(path);
writer.WriteLine(title + "\n" + output);
writer.Close();
}
}
Any pointers?
I Need save the txt file on a correct create folder. But its saving on C:\Nova Pasta i need save on "C:\Nova pasta\"+valor.retorna_nome+comboBox1.Text whats is wrong ?
private void btn_SaveFile_Click(object sender, EventArgs e)
{
objSQL.Search_RGP_CadastroPrint(Convert.ToInt32(comboBox1.Text), str_list);
objSQL.SearchPrint(Convert.ToInt32(comboBox1.Text));
string path = #"C:\Nova pasta\"+valor.retorna_nome+comboBox1.Text;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
StreamWriter file = new System.IO.StreamWriter(path + ".txt");
file.WriteLine("---------------------------------------------------------------------------------------------------------");
file.WriteLine("Nome: " + valor.retorna_nome);
file.WriteLine("RGP: " + comboBox1.Text);
file.WriteLine("Endereço: " + valor.retorna_endereco);
file.WriteLine("Telefone: " + valor.retorna_telefone + " Celular: " + valor.retorna_celular + "\r\n");
str_list.ForEach(file.WriteLine);
file.Close();
}
Say valor.retorna_nome is "hello", and comboBox1.Text is "world". Your code does the following:
string path = #"C:\Nova pasta\"+valor.retorna_nome+comboBox1.Text;
// -> path = "C:\Nova pasta\helloworld"
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
// -> created directory "C:\Nova pasta\helloworld"
}
StreamWriter file = new System.IO.StreamWriter(path + ".txt");
// -> writes to file "C:\Nova pasta\helloworld.txt"
So it's doing exactly what you told it to. What would you like the directory to be called? And the filename?
Your String path is equals to something like that : "C:\Nova pasta\aNameXXX"
where :
aName = valor.retorna_nome
XXX = Combobox1.Text
You create a directory, this must success, but after that your file path is :
path+.txt : "C:\Nova pasta\aNameXXX.txt"
it's creating a file named (aNameXXX.txt) next to your folder.
you need to add an "\" and a name to your file to make a path like : "C:\Nova pasta\aNameXXX\FILENAME.txt"
StreamWriter file = new System.IO.StreamWriter(path + "\" + FILENAME + ".txt");
Is there any way to modify the following simple code, so that once the file myBackup.csv is created and the code is run over and over again, it doesn't just re-write the same file and destroy the previous contents, but creates further files?
string localPath = somePath + Path.DirectorySeparatorChar + "myBackup.csv";
StreamWriter sw = new StreamWriter( localPath );
sw.WriteLine( "blah blah:" );
foreach ( var element in entryId )
sw.WriteLine( element );
localSaver.Close();
So you want to check if the file exists and choose another file name if the file exists?
string localPath = somePath + Path.DirectorySeparatorChar + "myBackup.csv";
int counter = 1;
while (File.Exists(localPath))
{
localPath = somePath + Path.DirectorySeparatorChar + "myBackup-" + counter + ".csv";
counter++;
}
Then use localPath in your StreamWriter constructor.
I have function in my app to edit the user details in that there is feature which allows us to edit the image in the picturebox.
What I am trying to achieve is if user choose to edit the image to another the app should delete the current image and use the new image and save to db. If users edit other details without touching picturebox then no changes should be made.
I have written below code but but it's not working can correct me or suggest better solution for what I am trying to achieve.
string picPath
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog.Filter = "Image files|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
string FileName = openFileDialog.FileName;
profilePicPictureBox.ImageLocation = FileName;
}
if (File.Exists(#"D:\Local Pictures\Users\" + fnameTextBox.Text + "_" + lnameTextBox.Text + "_" + userIdTextBox.Text + ".jpg"))
{
File.Delete(#"D:\Local Pictures\Users\" + fnameTextBox.Text + "_" + lnameTextBox.Text + "_" + userIdTextBox.Text + ".jpg");
picPath = (#"D:\Local Pictures\Users\" + fnameTextBox.Text + "_" + lnameTextBox.Text + "_" + userIdTextBox.Text + ".jpg");
profilePicPictureBox.Image.Save(picPath);
}
when i try to execute the above i am getting a exception on relative path
I did:
if (averagesListTextFile != null)
{
Directory.CreateDirectory(subDirectoryName);
File.Create(subDirectoryName + "\\" + averagesListTextFile + ".txt");
And then I want to do:
reader = new StreamReader(subDirectoryName + "\\" + averagesListTextFile + ".txt");
But I'm getting error say the file is in use by another process...And that happen only after I did the File.Create
File.Create returns a stream, so you need to dispose it:
using (File.Create(subDirectoryName + "\\" + averagesListTextFile + ".txt"))
{
}
or equivalently in this case:
File.Create(subDirectoryName + "\\" + averagesListTextFile + ".txt").Dispose();
But if you've just created the file, why would you try to read it? It will be empty.
Note that your reader should use a using statement too. Alternatively, to read and write complete text files, you should look into File.WriteAllText and File.ReadAllText, which make life simpler.