OpenFileDialog cannot read images - c#

When I use OpenFileDialog to select a file on my WPF project I got an error window saying:
You do not have permissions to open this file.
Request permissions from the file owner or an administrator.
This only happen when I use image format files, with other type of file the File Dialog work as intended.
I already tried to open the app as admin and the error persists.
Test code:
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "jpg |*.jpg|png |*.png";
fileDialog.ShowDialog();

Perhaps you could try a new way which is safe at finding what file types you are looking for:
var dlg = new Microsoft.Win32.OpenFileDialog
{
Title = "Select an Image",
Filter = "Images |*.jpg; *.png",
CheckFileExists = true
};
if (dlg.ShowDialog() == true)
{
}
On file permissions try this https://windowsreport.com/no-permission-open-file/

Related

Get complete file path from file click in File Explorer

Using Visual Studio 2017 and Windows 10 I want to be able to open a file explorer and navigate to a file outside of the program. Once my file is collected I want to get the file path and the complete file name for the file explorer.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
textBoxFolderpath.Text= openFileDialog1.FileName;
if (textBoxFolderpath.Text.Contains('\\'))
textBoxFolderpath.Text = textBoxFolderpath.Text.Substring(0, textBoxFolderpath.Text.LastIndexOf('\\'));
This is what I have tried so far. The textBoxFolderpath is being used to hold the values.
What am I missing or doing incorrectly?
When working with filenames and paths I'd highly recommend using built-in classes to handle this, like the Path class:
Path.GetDirectoryName(openFileDialog1.FileName)
This returns the path, without the filename. It's also cross-platform compatible.
Thanks Zer0 - below is what I ended up using.
private void btnCurrentFolder_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog1 = new OpenFileDialog();
// Determine starting directory
if (chkSetToRoot.Checked)
{
openDialog1.InitialDirectory = #"K:\RESULTS";
}
openDialog1.Title = "Select A Test File";
openDialog1.Filter = "All Files (*.*)|*.*";
if (openDialog1.ShowDialog() == DialogResult.OK)
{
textBoxFolderpath.Text = Path.GetDirectoryName(openDialog1.FileName);
textBoxFileName.Text = Path.GetFileName(openDialog1.FileName);
}
}

SaveFileDialog cannot find desktop folder path

I'm using SavefileDialog in C#. However, my SavefileDialog can't find the desktop folder path.
This is my code:
SaveFileDialog sfd = new SaveFileDialog();
DialogResult result = sfd.ShowDialog(this);
if( result == DialogResult.OK) {
// do something
}
Once SaveFileDialog is started, this error pops up:
Error: 'C:\Windows\system32\config\systemprofile\Desktop' refers to a location that is unavailable.
Why does the error pop up and how can I solve it?
To make SaveFileDialog open on particular directory, use InitialDirectory:
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
DialogResult result = sfd.ShowDialog(this);
if (result == DialogResult.OK)
{
// do something
}
As for your error, please provide more details or at least the whole code of the method.

Using SaveFileDialog in c# Winforms

I'm basically just trying to get a file path to save a file to but my SaveFileObject won't let me access the SelectedPath. I've checked the other forums and can't figure out why it won' tlet me, here's my code;
SaveFileDialog filePath = new SaveFileDialog();
DialogResult result = filePath.ShowDialog();
if (result == DialogResult.OK)
{
string folderPath = filePath.;
}
It'll let me select filePath.ShowDialog again and filePath.ToString etc... Where am I going wrong?
You actually want the file name from the FileName property from your SaveFileDialog. That will give you the full path and file name for the file your user wants to save.
SaveFileDialog saveDialog = new SaveFileDialog();
DialogResult result = saveDialog.ShowDialog();
if (result == DialogResult.OK)
{
String fileName = saveDialog.FileName;
//your code to save the file;
}
Although, since .ShowDialog() returns a DialogResult, you can use it directly in the if to spare one line of code (yup! I'm greedy)

How to disable OpenFileDialog opening files automatically?

I am using following code to select a file to import in a Windows Forms project.
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "C# Corner Open File Dialog";
fdlg.InitialDirectory = #"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
txtpath.Text = fdlg.FileName;
}
The problem is that the selected file is opened in the background which I don't want. What can I do to just get the path of selected file without opening it?
Showing an OpenFileDialog and the user selecting a file does not open the file. The file can be opened by calling OpenFile. In the code you posted the file is not opened. That code appears to be copied from an example on MSDN. The rest of the code from that example is here:
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null) // File is opened here.
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " +
ex.Message);
}
}
If the file is being opened when you don't want it opened then the problem must be somewhere else and not in the code you posted. It is possible for example that you have not closed the file (for example by using Dispose) after you finished using it last time.

File Open/Save Dialog

I am using my own Custom View to show the files and folders and also using a search box to jump to a specific folder. In that case How to send a message to File Open/Save dialog to enforce it to change the current displayed folder.
e.g. If the dialog shows files and folders of current displaying folder "C:\", I want an API (or any piece of code) to enforce to change the current folder to "D:\"
You can have the dialog open at a specific directory using InitialDirectory.
If you want to control what the dialog does at runtime, that's a bit more complex.
Set SaveFileDialog.InitialDirectory after you create it, but before you open it.
For example:
Stream myStream = null;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1 .InitialDirectory = "d:\\" ;
saveFileDialog1 .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1 .FilterIndex = 2 ;
saveFileDialog1 .RestoreDirectory = true ;
if(saveFileDialog1 .ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = saveFileDialog1 .OpenFile()) != null)
{
// Code to write the stream goes here.
myStream.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not save file to disk. Original error: " + ex.Message);
}
}
set InitialDirectory property to any path

Categories