I am using LinqToCSV to export list of values in C#, and it works fine when I use only two lines as
LINQtoCSV.CsvContext CSVContext = new CsvContext();
CSVContext.Write(bullishRowList, "C://FileName.CSV");
But I want to use SaveFileDialog to allow user to choose the location where he wants to save the file. I did the following for this purpose. I took this code from here
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "csv files (*.csv)|*.csv";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
LINQtoCSV.CsvContext CSVContext = new CsvContext();
CSVContext.Write(bullishRowList, saveFileDialog1.FileName);
myStream.Close();
}
}
It gives an error "The process cannot access the file 'C:\aaa' because it is being used by another process" on line CSVContext.Write(bullishRowList, saveFileDialog1.FileName); I can't figure out what is the problem with this piece of code.
please help me.
It is your own process that opens the file in this line
if ((myStream = saveFileDialog1.OpenFile()) != null)
and that line is not necessary
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog1.FileName != string.Empty)
{
LINQtoCSV.CsvContext CSVContext = new CsvContext();
CSVContext.Write(bullishRowList, saveFileDialog1.FileName);
}
}
I have never used that library, but looking briefly at their documentation, if you want to open yourself the stream, then you need to pass the opened stream to the Write method
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
LINQtoCSV.CsvContext CSVContext = new CsvContext();
CSVContext.Write(bullishRowList, myStream);
myStream.Close();
}
}
Related
private void SaveFile()
{
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
rtb1.Text().get();
}
}
}
I use this when trying to get the text of a richtextbox and save it but it always comes up with error, CS1955
If you just want to write a simple text file then you can use the File.WriteAllText command instead.
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.Filename, rtb1.Text);
}
You just need to specify the filename with path, and then the text, which in this case is rtb1.Text (no brackets!)
I created a new project in Windows Forms App (.NET Framework), this is the code I have:
private void SaveProxyResults_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
StreamWriter writer = new StreamWriter(dlg.FileName);
for (int i = 0; i < GatheredProxies.Items.Count; i++)
{
writer.WriteLine((string)GatheredProxies.Items[i]);
}
writer.Close();
}
dlg.Dispose();
}
With this current code the save file menu pops up but it doesn't automatically go to your Desktop and there's no file type chosen automatically, I also can't save it as .txt because it gives me an error.
How do I have to edit the code in order to make it automatically choose .txt as file format, be able to type in the file name and automatically select your Desktop as file saving location while still being able to change the location of where the file is supposed to be saved?
Stream myStream;
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dlg.FilterIndex = 2;
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == DialogResult.OK)
{
if ((myStream = dlg.OpenFile()) != null)
{
StreamWriter writer = new StreamWriter(dlg.FileName);
for (int i = 0; i < GatheredProxies.Items.Count; i++)
{
writer.WriteLine((string)GatheredProxies.Items[i]);
}
writer.Close();
}
dlg.Dispose();
}
Gives me the error:
System.IO.IOException: 'The process cannot access the file 'C:\Users\JP\Desktop\Scraped Proxies123' because it is being used by another process.'
According to your description, you want it to automatically select .txt as the file format
and be able to type the file name and automatically select the desktop as the file saving
location.
You can try the following code to solve this problem.
Stream myStream;
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "";
dlg.InitialDirectory = #"C:\Users\username\Desktop";// Use the absolute path of your computer desktop
dlg.Filter = "txt files (*.txt)|*.txt";
dlg.FilterIndex = 1;
if (dlg.ShowDialog() == DialogResult.OK)
{
if ((myStream = dlg.OpenFile()) != null)
{
myStream.Close();
StreamWriter writer = new StreamWriter(dlg.FileName);
for (int i = 0; i < GatheredProxies.Items.Count; i++)
{
writer.WriteLine((string)GatheredProxies.Items[i]);
}
writer.Close();
}
}
dlg.Dispose();
I have looked around on several answers to similar questions, but somehow this isnt working for me.
I am trying to save the contents of a textbox into a user promptet file.
private void btnSave_Click(object sender, EventArgs e)
{
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
File.WriteAllText(saveFileDialog1.FileName, rtbIncoming.Text);
myStream.Close();
}
}
}
The User prompt pops up as expected, and the file is generated but without any content.
You don't need to open the file stream yourself. File.WriteAllText() does all this for you. So this should be enough:
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
File.WriteAllText(saveFileDialog1.FileName, rtbIncoming.Text);
I guess your code leads to an empty file because you open a seperate stream that isn't used to write and closed (and flushed) after the call to WriteAllText().
I am trying to open a file by pressing a button (a label to be more exact but it works just the same way)
For some reason when the FileDialog opens and I select the file and press open it doesnt open the file it only closes the FileDialog
private void selectLbl_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = "c:\\";
ofd.Filter = "Script files (*.au3)|*.au3";
ofd.RestoreDirectory = true;
ofd.Title = ("Select Your Helping Script");
if (ofd.ShowDialog() == DialogResult.OK)
{
ofd.OpenFile(); //Not sure if there is supposed to be more here
}
}
ofd.OpenFile();
is returning the content of the file as Stream of bytes like described here. If you want to open the file like you described it, use
if (ofd.ShowDialog() == DialogResult.OK)
{
System.Diagnostics.Process.Start(ofd.FileName);
}
So your selected file starts with the associated application.
ofd.OpenFile() opens the file selected by the user as a Stream that you can use to read from the file.
What you do with that stream depends on what you are trying to achieve. For example, you can read and output all lines:
if (ofd.ShowDialog() == DialogResult.OK)
{
using (TextReader reader = new StreamReader(ofd.OpenFile()))
{
string line;
while((line = t.ReadLine()) != null)
Console.WriteLine(line);
}
}
Or if it is an xml file you can parse it as xml:
if (ofd.ShowDialog() == DialogResult.OK)
{
using(XmlTextReader t = new XmlTextReader(ofd.OpenFile()))
{
while (t.Read())
Console.WriteLine($"{t.Name}: {t.Value}");
}
}
The OpenFileDialog is not a dialog that opens the file. It only communicates with the operator with a dialog that is commonly shown if a program needs to know what file to open. So it is only a dialog box, not a file opener.
You decide what to do if the user pressed ok or cancel:
private void selectLbl_click(object sender, ...)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.InitialDirectory = "c:\\";
ofd.Filter = "Script files (*.au3)|*.au3";
ofd.RestoreDirectory = true;
ofd.Title = ("Select Your Helping Script");
var dlgResult = ofd.ShowDialog(this);
if (dlgResult == DialogResult.OK)
{ // operator pressed OK, get the filename:
string fullFileName = ofd.FileName;
ProcessFile(fullFileName);
}
}
}
if (ofd.ShowDialog() == DialogResult.OK)
{
ofd.OpenFile(); //Not sure if there is supposed to be more here
}
I'm trying to write to a text file, and I'm using the example from MSDN but I can't figure out how to add text to it?
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream ;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
myStream.Close();
}
}
}
I've changed // Code to write the stream goes here. to a couple of things, like:
string thing = "This gets written";
and
"This gets written";
But that didn't work and I have no other ideas :L
using (var myStream = saveFileDialog1.OpenFile())
using (var writer = new StreamWriter(myStream))
{
writer.WriteLine("File content goes here");
}