I know how to create and save a file in C# console application but what if i want the user to choose the location of where they want to save it? i have no idea on how i can make this possible.
edit- ive realised that would be very hard to input the location the user wants to save the file to however is it possible to save the file as you would do when creating a windows word document, so the user would be able to see where they want to save the file Example
If you want a full console application, that is no windows being created, then there's only one proper thing to do: require the user to specify the save location on the command line.*
Given the fact that you have a console application, you probably already do some checking of the command line, but if not, then the command line can be read from the args
argument to your Program.Main
:
static void Main(string[] args)
{
...
}
There are examples on the internet to handle the command line, if you get stuck, or a new question can be asked specifically for the issue you are having.
*) Now, for the reason why this is the only proper way:
If the user needs to pass it on the command line, then the user has all the usual niceties such as tab completion available. The user can also use dir
and cd
before calling your program to find the proper directory.
On the other hand, if you ask the user to input it, then the user will not have tab completion, will not be able to use dir
or cd
, and as such will have to type it out manually all the way. Typo's or mistakes are almost guaranteed.
From a user experience point of view, this is very annoying. Programmers should therefore not ask the user to manually type out file paths during program execution. Hence, it must be specified on the command line.
Read about SaveFileDialog
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();
}
}
}
And ready to test example from msdn example
if you want to play with something like >cd (ChDir) then look at
Environment.CurrentDirectory
or/and force user to write good directory by himselft and use @Carl aswere :) but remember we leave in 21 century people are lazy
It is not hard. You need a console to read user's input with string path = Console.ReadLine();
. User will input prefered path and it will store into path
variable. Now you should check if this path exits if(Directory.Exists(path))
it will return true if path exists false if path does not exist.
Code example:
Console.WriteLine("Insert a path: ");
string path = Console.ReadLine();
if(Directory.Exists(path)){
//save logic
}
else{
//path does not exist handler
}
Note: if you want to access Directory class you should use System.IO
namespace.
You can use the SaveFileDialog as Taumantis said but you have to add the
System.Windows.Froms
namespace and you must mark your main method as a single apartment thread
class Program
{
[STAThread]
static void Main(string[] args)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
}
Console.ReadKey();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.