SaveFileDialog not showing existing files - winforms

I've got a SaveFileDialog that doesn't show the existing pdf files when I have ".pdf File" seleted. Why is that? Any existing png files show up just fine when I've got ".png Image" selected in the dialog, so I'm not sure why only the existing pdf files aren't showing up.
SaveFileDialog sfd = new SaveFileDialog();
sfd.AddExtension = true;
sfd.Filter = ".pdf File | *.pdf |.png Image | *.png";
sfd.CheckPathExists = true;
sfd.OverwritePrompt = true;
sfd.SupportMultiDottedExtensions = false;
sfd.ValidateNames = true;
sfd.ShowDialog();
I apologize if this is a duplicate question, but I couldn't find it asked anywhere despite thinking that this would be a common problem.

You should remove the spaces in the extension passed to the Filter property
sfd.Filter = ".pdf File|*.pdf|.png Image|*.png";

Related

Folder acess denied wpf

I am opening a FileDialog, to let the user select an Audio file, in an MP3 file, and convert it to WAV, the error appears when I am trying to save the file in a new folder that I am creating
var dlg = new OpenFileDialog
{
DefaultExt = ".mp3",
Filter = "Audio files (.mp3)|*.mp3"
};
var res = dlg.ShowDialog();
if (res! == true)
{
var projectPath = Directory.GetParent(Directory.GetCurrentDirectory())?.Parent?.Parent?.FullName;
var FoderName = Path.Combine(projectPath!, "Audios");
Directory.CreateDirectory(FoderName);
using (var mp3 = new Mp3FileReader(dlg.FileName))
{
using (var ws = WaveFormatConversionStream.CreatePcmStream(mp3))
{
WaveFileWriter.CreateWaveFile(FoderName, was) // Error
System.UnauthorizedAccessException: 'Access to the path
'C:\XXX\XXX\XXX\XXX\XXX\XXX' is denied.'
}
}
Thanks
Inside some folders including "Program Files", an app usually has no permission to write a file. Therefore, it is recommended to use a folder dedicated for a specific purpose and made available for apps (you can get path to "Music" by Environment.GetFolderPath(Environment.SpecialFolder.MyMusic).
In general, an app must expect UnauthorizedAccessException when attempting to write a file and prepare a fallback for the case of that exception.

XAML OpenFileDialog(), missing namespace

I recently started doing some WPF windows 8.1 apps and now I searched for a open file dialog. Some information is available at Microsoft and some blogs but the results are not that great.
Tried these lines:
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
string filename = dlg.FileName;
}
This should work greatly as there are way more examples like this one, unfortunately not in my case because of the missing assembly reference.. It does not know Win32 in the Microsoft using.
If in any way someone can clear this out to me, I would appreciate it.

How to upload file in silverlight

I want to save (upload) a file in silverlight into the silverlight application folder.
I get the URI of the application
string str3 = App.Current.Host.Source.AbsoluteUri + "/Recording/";
but i don't know how to save file.
I use this code.....
string extension = "wav";
// Create an instance of the open file dialog box.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Filter = String.Format("{1} files (*.{0})|*.{0}|WAV FILES (*.*)|*.*", extension, "Audio");
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false;
// Call the ShowDialog method to show the dialog box.
bool? userClickedOK = openFileDialog1.ShowDialog();
// Process input if the user clicked OK.
if (userClickedOK == true)
{
string str = App.Current.Host.Source.AbsoluteUri + "/Recording/";
openFileDialog1.File.CopyTo(str);
}
That won't work by itself (I assume your application is hosted on a web server), you need an uploader that will send the content to the server, and a server side handler that will receive and store the file.
Take you pick from one of those Silverlight uploaders:
Silverlight File Uploader
Silverlight File Upload
Silverlight Multi File Uploader
Personally I went with the first one, but I believe they're all quite good.

open a PDF : WPF

I want to open a PDF file in a button click. I'll keep the PDF file within the solution/namespace of the project. Can anyone give me solution for this?
To start the standard PDF viewer you can simply start an external process:
Process proc = new Process( );
proc.StartInfo = new ProcessStartInfo( ) {
FileName = path //put your path here
};
proc.Start( );
To show the file inside your application you have to use the pdf viewer as an ActiveX-component.
My solution:
private AxAcroPDFLib.AxAcroPDF axAcroPDF1;
this.axAcroPDF1 = new AxAcroPDFLib.AxAcroPDF();
this.axAcroPDF1.Dock = System.Windows.Forms.DockStyle.Fill;
this.axAcroPDF1.Enabled = true;
this.axAcroPDF1.Name = "axAcroPDF1";
this.axAcroPDF1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axAcroPDF1.OcxState")));
axAcroPDF1.LoadFile(DownloadedFullFileName);
axAcroPDF1.Visible = true;
You have many options:
User WPF WebBrowser control and open the pdf:
Link 1, Link 2
User WinformsHost to host ActiveX Stackoverflow link.

Relative Uri works for BitmapImage, but not for MediaPlayer?

This will be simple for you guys:
var uri = new Uri("pack://application:,,,/LiftExperiment;component/pics/outside/elevator.jpg");
imageBitmap = new BitmapImage();
imageBitmap.BeginInit();
imageBitmap.UriSource = uri;
imageBitmap.EndInit();
image.Source = imageBitmap;
=> Works perfectly on a .jpg with
Build Action: Content
Copy to Output Directory: Copy always
MediaPlayer mp = new MediaPlayer();
var uri = new Uri("pack://application:,,,/LiftExperiment;component/sounds/DialingTone.wav");
mp.Open(uri);
mp.Play();
=> Does not work on a .wav with the same build action and copy to output. I see the file in my /debug/ folder..
MediaPlayer mp = new MediaPlayer();
var uri = new Uri(#"E:\projects\LiftExp\_solution\LiftExperiment\bin\Debug\sounds\DialingTone.wav");
mp.Open(uri);
mp.Play();
=> Works perfectly..
So, how do I get the sound to work with a relative path? Why is it not working this way?
Let me know if you want more code or screenshots.
Thanks.
The pack://application URI syntax is for "embed" files, make sure the the media file is set to that, or use the pack://siteoforigin for "loose" files (copied to bin directory).
MSDN link

Resources