Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get full path from savefiledialog and use in "startInfo.Arguments"?

Tags:

c#

winforms

In my case the SaveFileDialog will not write any file, but I want to use to specify the path for a command line app which will create the logfile on the same location as "saved" in the sf dialog.

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "*.txt";
string sfdname = saveFileDialog1.FileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
  Path.GetFileName(sfd.FileName);
}

startInfo.Arguments = "--log=" + Path.GetFileName(sfd.FileName);
like image 278
user830054 Avatar asked Dec 07 '11 14:12

user830054


People also ask

How to get selected Path of SaveFileDialog c#?

Here is the code: SaveFileDialog sfd = new SaveFileDialog(); sfd. Filter = "Xml files (*. xml)|*.

How we can save a file using SaveFileDialog control?

To save a file using the SaveFileDialog component. Display the Save File dialog box and call a method to save the file selected by the user. Use the SaveFileDialog component's OpenFile method to save the file. This method gives you a Stream object you can write to.


2 Answers

You can use

Path.GetFullPath(sfd.FileName);

Instead of

Path.GetFileName(sfd.FileName);

Complete version...

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "*.txt";
string sfdname = saveFileDialog1.FileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
  Path.GetFullPath(sfd.FileName);
}

startInfo.Arguments = "--log=" + Path.GetFullPath(sfd.FileName);
like image 194
Wanderson López Avatar answered Oct 16 '22 10:10

Wanderson López


OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Image files | *.jpg";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                 employee_dp.Image = Image.FromFile(openFileDialog1.FileName);
                 string path = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
                 string onlyFileName = System.IO.Path.GetFileName(openFileDialog1.FileName);
                 filepath = Path.GetFullPath(path).Replace(@"\", @"\\");
                 filepath = filepath + "\\\\" + onlyFileName;
                 MessageBox.Show(filepath);
like image 28
royhette agapito Avatar answered Oct 16 '22 10:10

royhette agapito