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);
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);
Just remove the Path.GetFileName:
startInfo.Arguments = "--log=\"" + sfd.FileName + "\"";
I think you are using the wrong dialog form based on what you are describing.
Try using the FolderBrowserDialog class:
string folderPath = string.Empty;
using (FolderBrowserDialog fdb = new FolderBrowserDialog()) {
if (fdb.ShowDialog() == DialogResult.OK ){
folderPath = fdb.SelectedPath;
}
}
if (folderPath != string.Empty) {
startInfo.Arguments = "--log=" + folderPath;
}
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);
Perhaps Path.GetFullPath would help?
The problem might be using the wrong FileSaveDialog.
The one in Win32.dll doesn't provide the full path, but the one in System.Windows.Forms does.
Related
Is there a way to set filenames from OpenFileDialog through code?
string[] files = {"D:\\test1.txt","D:\\test2.txt"};
System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
dialog.Multiselect = true;
dialog.FileNames = files;
Error while trying to set the FileNames:
Error CS0200 Property or indexer 'FileDialog.FileNames' cannot be assigned to -- it is read only
Actually, you can use InitialDirectory to set the default address
openFileDialog.InitialDirectory = "D:\\";
and use openFileDialog.Filter to set specific type
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
and you can use openFileDialog1.FileName to select a file
openFileDialog1.FileName = "test1"
or muliplefile
openFileDialog1.FileName = "\"test1\" \"test2\"";
For example:
string message = "";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt";
openFileDialog1.FileName = "\"test1\"\"test2\"";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (string file in openFileDialog1.FileNames)
{
message += Path.GetFileName(file) + " - " + file + Environment.NewLine;
}
MessageBox.Show(message);
}
Hy everyone,
I would like to copy multiple selected files with openfiledialog to a folder which is defined as #"C:\TestFolder\"+ textBox1.Text. My problem is that somehow the program writes the textBox content in the file name too.
Please find below my code:
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog od = new OpenFileDialog();
od.Filter = "All files (*.*)|*.*";
od.Multiselect = true;
if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string targetPath = #"C:\TestFolder\"+ textBox1.Text;
string path = System.IO.Path.Combine(targetPath, textBox1.Text);
if (!System.IO.Directory.Exists(targetPath)
{
System.IO.Directory.CreateDirectory(targetPath);
}
foreach (string fileName in od.FileNames)
{
System.IO.File.Copy(fileName, path + System.IO.Path.GetFileName(fileName));
}
}
}
Any input would be appreciated!
Try this one:
string Main_dir = #"C:\TestFolder\";
string Sub_dir = textBox1.Text + #"\";
string targetPath = System.IO.Path.Combine(Main_dir, Sub_dir);
{
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
foreach (string fileName in od.FileNames)
System.IO.File.Copy(fileName, targetPath + System.IO.Path.GetFileName(fileName), true);
}
Backslash is missing
#"\"
These things are equivalent.
string targetPath = #"C:\TestFolder\"+ textBox1.Text;
string path = System.IO.Path.Combine(targetPath, textBox1.Text);
I would drop the first one for the Path.Combine call as it is portable and robust when it comes to the separators.
So I recently tried to the FolderBrowserDialog but much to my disappointment it was not like the following screenshot:
But instead it was formatted and as I think, hard to navigate with like this:
How would I go about getting the other version where it's a dialog box asking for what folder to save to like the select file type natively, instead of what I think is this hard to navigate menu.
The CommonOpenFileDialog class from the NuGet Package "Microsoft.WindowsAPICodePack-Shell" will answer your request.
Set IsFolderPicker property to true and that's it.
using Microsoft.WindowsAPICodePack.Dialogs;
private bool SelectFolder(out string fileName)
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
fileName = dialog.FileName;
return true;
}
else
{
fileName = "";
return false;
}
}
thats because you are using FolderBrowserDialog instead of OpenFileDialog
you can check the below
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "Browse File";
fileDialog.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fileDialog.FilterIndex = 2;
fileDialog.InitialDirectory = "c:\\";
fileDialog.RestoreDirectory = true;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = fileDialog.FileName;
}
}
I am fairly new to c#.
My question is what is strFileName in open file dialog?
I have this code currently:
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
It brings up an error on strFileName. i cant find an explanation as to what it does in this code.
Any help will be appreciated and i apologise if this question has been asked before.
Without knowing what the error is, just by looking at your code, you are probably getting a compile error on strFileName because it is not declared:
You can either change your code to this:
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
input = open.FileName;
if (input == String.Empty)
return;
Or this:
string strFileName = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
It doesn't give me an error - though I did have to declare it. Did you?
string strFileName = ""; // added this line - it compiles and runs ok
private void TestFunc()
{
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
}
you need declare strFileName first
string strFileName = string.empty();
then use it.
You need to declare the variable strFilename as a string:
string strFileName = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
{
strFileName = open.FileName;
}
/* you probably don't want this unless it's part of a larger block of code
if (strFileName == String.Empty)
{
return;
}
*/
return strFileName;
I'm new to programming and I'm starting to create a simple notepad, with only 4 buttons (Open, Save, New and Font).
If I open or save I'm getting an error:
This is my code:
//Declare save as a new SaveFileDailog
SaveFileDialog save = new SaveFileDialog();
//Declare filename as a String equal to the SaveFileDialog's FileName
String filename = save.FileName;
//Declare filter as a String equal to our wanted SaveFileDialog Filter
String filter = "Text Files|*.txt|All Files|*.*";
//Set the SaveFileDialog's Filter to filter
save.Filter = filter;
//Set the title of the SaveFileDialog to Save
save.Title = "Save";
//Show the SaveFileDialog
if (save.ShowDialog(this) == DialogResult.OK)
{
//Write all of the text in txtBox to the specified file
System.IO.File.WriteAllText(filename, textBox1.Text);
}
else
{
//Return
return;
}//Declare save as a new SaveFileDailog
SaveFileDialog save = new SaveFileDialog();
//Declare filename as a String equal to the SaveFileDialog's FileName
String filename = save.FileName;
//Declare filter as a String equal to our wanted SaveFileDialog Filter
String filter = "Text Files|*.txt|All Files|*.*";
//Set the SaveFileDialog's Filter to filter
save.Filter = filter;
//Set the title of the SaveFileDialog to Save
save.Title = "Save";
//Show the SaveFileDialog
if (save.ShowDialog(this) == DialogResult.OK)
{
//Write all of the text in txtBox to the specified file
System.IO.File.WriteAllText(filename, textBox1.Text);
}
else
{
//Return
return;
}
Any idea? Thanks and regards
ooopss I forgot to write the error sorry about that:
Here is the error:
"Error: ArgumentException was unhandled.
Empty path name is not legal"
I get this if I open a text file. Then it highlighted this line code:
textBox1.Text=System.IO.File.ReadAllText(filename,System.Text.Encoding.Default);
And if I save nothing happens.
Thanks
I expect you should be reading the filename after the user has used the dialog:
System.IO.File.WriteAllText(save.FileName, textBox1.Text);
Also - SaveFileDialog is IDisposable, so you should be "using" it...
using (SaveFileDialog save = new SaveFileDialog())
{
// your code that involves "save"
}
Try moving the line
String filename = save.FileName;
inside the IF block.
You are assigning to filename before the SaveDialog's property is set by the user.
You need to understand that this line does not create a permanent link between your filename variable and the FileName property of the dialog.
You get the filename from a SaveFileDialog after you call ShowDialog. You are setting filename beforehand.
Well, it looks like you're saving with a blank filename - this changes during the call to .ShowDialog(), so it doesnt help that you've retrieved it beforehand.
You just need to pull out .FileName again after .ShowDialog.
//To declare private int docno
//To declare private string filename
//To declare private bool modified=false;
if (modify == true)
{
//this.Text = filename;
filename = saveFileDialog1.FileName;
sw = new StreamWriter(filename);
sw.Write(textBox1.Text);
sw.Close();
//modify = false;
}
else
{
saveFileDialog1.FileName = "Untitled" + docno.ToString() + ".txt";
dresult = saveFileDialog1.ShowDialog();
docno++;
}