XAML OpenFileDialog(), missing namespace - wpf

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.

Related

SaveFileDialog not showing existing files

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";

Application showing an exception"file not found "

I created an application in VS2010 with operating system WINDOWS XP.
Now, I updated the os to WIN 7 and also updated the location of the application.
So, while running the application for opening a file using open dialog box it showing some exception like"File Not Found".
It was working fine with WIN XP, but now it showing this error, if we keep that perticular file in bin folder its working fine , but if we choose a file from other drive or a folder it showing error.
enter code here
string chosen_file = "";
ofd.Title = "Open a file";
ofd.FileName = "";
ofd.Filter = "Text Files(*.txt)|*.txt|Rich Text Box(*.rtb)|*.rtb|Word Document(*.doc)|*.doc|HTML Pages(*.htm)|*.html|Cascading Style Sheet(*.css)|*.css|JAVA(*.java)|*.java|video file(*.wmv)|*.wmv|All Files(*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
chosen_file = ofd.FileName;
// richTextBox2.LoadFile(chosen_file, RichTextBoxStreamType.PlainText);
var fileInfo = new FileInfo(ofd.FileName);
fileInfo.Length.ToString();
byte[] buffer = new byte[fileInfo.Length];
int length = (int)fileInfo.Length;
FileStream fileStream = new FileStream(fileInfo.Name, FileMode.Open, FileAccess.Read);
fileStream.Read(buffer, 0, length);}
My guess is you referenced a hard-coded path, probably to your "Documents" folder, rather than using an environment variable. With the change from XP to 7, that directory changed. I can't recall what the directory was for Windows XP, but it's now /users/username for Windows 7. Either way, you would be better off using an environment variable.
Look through your program for Documents and Settings and see if you can find it. If you go to a command prompt and type set, it should give you a list of environment variables you could use in place of whatever you were using.

How to disable file in use check in WPF OpenFileDialog?

My WPF app is using the Microsoft.Win32.OpenFileDialog to select a SQL Server 2008 database to open.
It works OK but for one issue: When the database selected in the dialog was previously opened at some time since last boot, the file seems to be held open by SQL server in the background (even when it is not opened by my app and my app has been restarted). This causes a "file is used by another application" warning when OK is clicked in the OpenFileDialog and i can not use the dialog to open that particular database until the computer is rebooted. It seems the OpenFileDialog tries to open the file selected and doing that discovers that it is already opened by another app (SQL Server). How do i disable the OpenFileDialog from trying to open the selected file and just return the filename of the selected file without any checks?
My code looks like this:
public void OpenDatabase() {
// Let user select database to open from file browser dialog
// Configure open file dialog box
var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = ""; // Default file name
dlg.DefaultExt = ".mdf"; // Default file extension
dlg.Filter = "Databases (.mdf)|*.mdf|All Files|*.*"; // Filter files by extension
dlg.CheckFileExists = false;
dlg.CheckPathExists = false;
// Show open file dialog box
bool? result = dlg.ShowDialog(); // Gives file in use warning second time!
// Process open file dialog box results
if (result == true) {
// Open document
string filename = dlg.FileName;
TryOpenDatabase(filename);
}
}
The underlying option is OFN_NOVALIDATE for early Windows versions, FOS_NOVALIDATE for the Vista dialog you get on later versions of Windows and .NET. The description from MSDN:
Do not check for situations that would prevent an application from opening the selected file, such as sharing violations or access denied errors.
Which is what you see happening now, the dialog sees a sharing violation on the database file. This option is in fact exposed on the OpenFileDialog wrapper class, add this line of code to fix your problem:
dlg.ValidateNames = false;
The MSDN forum has a post about this
It is in the OpenFileDialog API, you can turn that off using
ValidateNames = false
on the Dialog.

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.

Sharepoint 2010 Upload file using Silverlight 4.0

I am trying to do a file upload from Silverlight(Client Object Model) to Sharepoint 2010 library.. Please see the code below..
try{
context = new ClientContext("http://deepu-pc/");
web = context.Web;
context.Load(web);
OpenFileDialog oFileDialog = new OpenFileDialog();
oFileDialog.FilterIndex = 1;
oFileDialog.Multiselect = false;
if (oFileDialog.ShowDialog().Value == true)
{
var localFile = new FileCreationInformation();
localFile.Content = System.IO.File.ReadAllBytes(oFileDialog.File.FullName);
localFile.Url = System.IO.Path.GetFileName(oFileDialog.File.Name);
List docs = web.Lists.GetByTitle("Gallery");
context.Load(docs);
File file = docs.RootFolder.Files.Add(localFile);
context.Load(file);
context.ExecuteQueryAsync(OnSiteLoadSuccess, OnSiteLoadFailure);
}
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
But I am getting the following error
System.Security.SecurityException: File operation not permitted. Access to path '' is denied.
at System.IO.FileSecurityState.EnsureState()
at System.IO.FileSystemInfo.get_FullName()
at ImageUploadSilverlight.MainPage.FileUpload_Click(Object sender, RoutedEventArgs e)
Any help would be appreciated
Thanks
Deepu
Silverlight runs with very restricted access to the client user's filesystem. When using an open-file dialog, you can get the name of the selected file within its parent folder, the length of the file, and a stream from which to read the data in the file, but not much more than that. You can't read the full path of the file selected, and you are getting the exception because you are attempting to do precisely that.
If you want to read the entire content of the file into a byte array, you'll have to replace the line
localFile.Content = System.IO.File.ReadAllBytes(oFileDialog.File.FullName);
with something like
localFile.content = ReadFully(oFileDialog.File.OpenRead());
The ReadFully method reads the entire content of a stream into a byte array. It's not a standard Silverlight method; instead it
is taken from this answer. (I gave this method a quick test on Silverlight, and it appears to work.)

Resources