Stream scriptelfen;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if((scriptelfen = openFileDialog1.OpenFile())!= null)
{
string strfilename = openFileDialog1.FileName;
string filetext = File.ReadAllText(strfilename);
script_box.Text = filetext;
When I opened my file, it is displaying text or hex code extension. I want to be able to push save file. I don't have to do a save file dialog is this possible at all ?
WriteAllText in MSDN
File.WriteAllText("C:\file.txt", "your content.");
Related
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 am so lost.... how could I make this set of code save the user input into another folder? (Im trying to save .png files if you need to know that)
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)
{
myStream.Close();
}
}
}
There is also some other and or additional code that you would really need to write for example if the user selects a file from one folder, then you could just capture that FilePath using System.IO and look up how to use GetFilePath , FileName, etc... then once the file is selected that they want to save ..why not use the File.Copy() method. there are plenty of additional examples online here on SO as well
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using( Stream myStream = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
using( StreamWriter sw = new TextWriter(myStream) )
{
sw.Write("here you can write lines from a file that you read or you can simple write what ever text you are wanting to save to a file this should help you get started" );
}
}
Here is an even simpler way of doing it
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.File.Copy(saveFileDialog1.Filename, "some dest filePath");
}
So I got the hang of doing OpenFileDialog, now I can't seem to understand SaveFileDialog. Looked at a few pages and each of them have there own ways of doing it, but none of them get down to the point of saving the text that is in the richtextbox to a file.
private void button1_Click(object sender, EventArgs e)
{
Stream myStream;
SaveFileDialog exportdialogue = new SaveFileDialog();
exportdialogue.Filter = "txt files (*.txt)|*.txt*";
exportdialogue.FilterIndex = 2;
exportdialogue.RestoreDirectory = true;
if (exportdialogue.ShowDialog() == DialogResult.OK)
{
if ((myStream = exportdialogue.OpenFile()) != null)
{
StreamWriter wText = new StreamWriter(myStream);
wText.Write("Some Text");
myStream.Close();
}
}
}
Using a richtextbox, and a normal button, also "using System.IO;" (For the Stream )
I am trying to get the button to use SaveFileDialog so it can export the content within the richtextbox to a text file.
Issue:
Unsure what I need to do from here to make it save contents from the rich text box.
Don't know why SaveFileDialog saves files with no extension when a filter is in place.
You set:
exportdialogue.Filter = "txt files (*.txt)|*.txt*";
which only contains ONE filter, but you set:
exportdialogue.FilterIndex = 2;
which means to use the SECOND filter. (FilterIndex is 1-based).
If you set FilterIndex = 1, your file should have the extension .txt
you can use using {} block to solve the issue:
Try this:
SaveFileDialog exportdialogue = new SaveFileDialog();
exportdialogue.Filter = "txt files (*.txt)|*.txt*";
exportdialogue.FilterIndex = 2;
exportdialogue.RestoreDirectory = true;
if (exportdialogue.ShowDialog() == DialogResult.OK)
{
using( Stream myStream = exportdialogue.OpenFile())
{
StreamWriter wText = new StreamWriter(myStream);
wText.Write("Some Text");
wText.Close();
}
}
you need to use savefile method of reach textbox and pass to it the filename from savedialogbox.
reachtextbox.SaveFile(exportdialogue.FileName);
ps: it will be something like above code.
I am creating a basic text editing piece of software in C# using Visual Studio 2012.
I would like to display the name of the open file in a label.
Currently, my OpenFileDialog code consists of:
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
try
{
richTextBoxPrintCtrl1.Text = ofd.FileName;
StreamReader sr = new StreamReader(richTextBoxPrintCtrl1.Text);
richTextBoxPrintCtrl1.Text = sr.ReadToEnd();
sr.Close();
richTextBoxPrintCtrl1.LoadFile(ofd.FileName, RichTextBoxStreamType.RichText);
}
catch { }
}
Let's say for example, I open Document.rtf using this software. How can I display that "Document.rtf" or any other open file title in a label (named filename1)?
use Path.GetFileName Method
string fileName = #"C:\mydir\myfile.ext";
string result = Path.GetFileName(fileName);
Console.WriteLine(result); // outputs myfile.ext
Path.GetFileName Method
UPDATE 1
string fileName = ofd.FileName;
richTextBoxPrintCtrl1.LoadFile(fileName, RichTextBoxStreamType.RichText);
label1.Text = Path.GetFileName(fileName); // here's your label
First, check that the user actually selects a file in the OpenFileDialog. Then set the text:
OpenFileDialog ofd = new OpenFileDialog();
// make sure user selects a file
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
try{
// load contents
richTextBoxPrintCtrl1.LoadFile(ofd.FileName, RichTextBoxStreamType.RichText);
// update label with file name
filename1.Text = System.IO.Path.GetFileName(ofd.FileName);
}catch{
// handle exception as you wish
}
}
I've got an application which opens a csv file and displays all the contents into a formatted datagridview. From there I have a button which opens another form that contains a series of checkboxes. The check boxes have all the attributes of the csv file we opened before, and the user is supposed to be able to query the file based on witch attributes they want, then save the file.
For example, if they only want a file which displays all the entries for animals with wings, they select the wings check box only. From there, you select the save button and it's supposed to save the file.
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
const string filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
const string header = "Animal_Name,Hair,Feathers,Eggs,Milk,Airborne,Aquatic,Predator,Toothed,Backbone,Breathes,Venomous,Fins,Legs,Tail,Domestic,Catsize,Type";
StreamWriter writer = null;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
filter = saveFileDialog1.FileName;
writer = new StreamWriter(filter);
writer.WriteLine(header);
foreach (Animal animal in animalQuery)
{
writer.Write(animal);
}
writer.Close();
}
}
This is the code for the save button, but there are errors under:
filter = saveFileDialog1.FileName;
writer = new StreamWriter(filter);
I'm not sure why.
unless your code is exact, you cannot assign to a constant variable for your code saying:
filter = saveFileDialog1.FileName;
You declared "filter" as a constant variable further up:
const string filter = "CSV file (.csv)|.csv| All Files (.)|.";
Try that:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
string filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
saveFileDialog1.Filter = filter;
const string header = "Animal_Name,Hair,Feathers,Eggs,Milk,Airborne,Aquatic,Predator,Toothed,Backbone,Breathes,Venomous,Fins,Legs,Tail,Domestic,Catsize,Type";
StreamWriter writer = null;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
filter = saveFileDialog1.FileName;
writer = new StreamWriter(filter);
writer.WriteLine(header);
writer.Close();
}
You use the SavefileDialog property "Filter" to define your list to filter by.