OpenFileDialog: How to copy files in a local folder? - c#

In my silverlight application I would like to be able to select a file from an OpenFileDialog window and upload/copy it to a local folder in my Silverlight project. I am already able to setup a OpenFileDialog window and set some options to it, but unfortunately I can't find a way to create a filestream and then copy it to a local folder.
private void Change_Avatar_Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Multiselect = false;
openfile.Filter = "Images files (*.bmp, *.png)|*.bmp;*.png";
if ((bool)openfile.ShowDialog())
{
}
}
I've tried looking at many tutorials on the net, but they only seem to send the file directly to the UploadFile method in silverlight what I do not want to do at the moment.
Thank you, Ephismen.

You can't just write files to local folders without prompting the user a second time (e.g. save as dialog http://www.silverlightshow.net/items/Using-the-SaveFileDialog-in-Silverlight-3.aspx)
You could write it to isolated storage instead: http://blogs.silverlight.net/blogs/msnow/archive/2009/05/21/71909.aspx.
If you want specific examples (e.g. going straight from OpenFileDialog to Isolated storage) I strongly recommend you use Google. The first match on "silverlight openfiledialog to isolated storage" is this: http://forums.silverlight.net/forums/t/201362.aspx

Related

How Read File Path From a .dat file then open the program When Pressing The Launch Button?

I have a problem Where I cant make my program automatically read the given file path inside the .dat and be ready to launch the program when pressing launch file without opening openFileDialog and choosing the program every time.
the code im using here is for the user to enter the file path for the first time then create a file path .dat file and it works with now issues.
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string path = Path.Combine(desktop, "LS\\Fail-SafePath.dat");
openFileDialog.InitialDirectory = filePath;
openFileDialog.Filter = " PlayGTAV (*.exe)|PlayGTAV.exe";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filePath = openFileDialog.FileName;
var fileStream = openFileDialog.OpenFile();
using (StreamReader reader = new StreamReader(fileStream))
{
fileContent = reader.ReadToEnd();
}
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(filePath);
}
After that that i have a start button for it
private void panel21_MouseClick(object sender, MouseEventArgs e)
{
Process.Start(filePath);
}
This works well when the user does it for the first time but now I want it to read that .dat file path automatically without having to ask the user for the file path every single time which I don't know how to do and need help with please.
I was thinking to do it like that: When Pressing the Launch button (After the first time) The Program Checks if the Fail-SafePath.dat Exists if Yes it reads the lines from it and starts the program from the given path without opening OpenFileDialog.
I'm Using Visual Studio, Windows Form.
If it's a file that the application will always need, then something like you mentioned:
I was thinking to do it like that: When Pressing the Launch button (After the first time) The Program Checks if the Fail-SafePath.dat Exists if Yes it reads the lines from it and starts the program from the given path without opening OpenFileDialog.
Could work easily enough. You could have your application look for it in the default location, and if not there, have your user select it.
Another solution could be using something like Application Settings or User Settings, which are values persisted between executions of .NET projects.
Depending on your full application design, you could also have the file path and other settings stored in some database or other data storage. There are a lot of ways to accomplish this.
EDIT: To elaborate further on the Application Settings
The application settings are very easy to read and write to.
You just need to create the ones you want, before trying to use them.
They can be created by:
Open Visual Studio.
In Solution Explorer, expand the Properties node of your project.
Double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings.
In the Settings designer, set the Name, Value, Type, and Scope for your setting. Each row represents a single setting.
To read from your settings:
this.FilePath= Properties.Settings.Default.FilePath;
To write to and save the setting:
Properties.Settings.Default.FilePath= Path.GetFullPath("importantFilePath");
Properties.Settings.Default.Save();

Detect when OpenFileDialog returns a downloaded URL/URI

I'm using OpenFileDialog (.Net Framework 4, Windows 10) and I've noticed that it will allow the user to specify a URL as the file name (e.g., http://somewebsite/picture.jpg). This is very useful for my application, so I don't intend to disable it. The way it works is downloading the file into the user's temp directory and returning the temporary file name in the dialog's Filename property. This is nice, except for the fact that the user starts to build up garbage in his/her temp directory.
I would like to tell when a file was downloaded by the OpenFileDialog class (as opposed to a previously existing file), so I can clean up by deleting the file after use. I could check if the file's directory is the temp directory, but that's not very good since the user might have downloaded the file him/herself.
I've tried intercepting the FileOK event and inspect the Filename property to see if it is an HTTP/FTP URI, but despite what the documentation says ("Occurs when the user selects a file name by either clicking the Open button of the OpenFileDialog") it is fired after the file is downloaded, so I don't get access to the URL: the Filename property already has the temporary file name.
EDIT: This is an example of what I'like to do:
Dim dlgOpenFile As New System.Windows.Forms.OpenFileDialog
If dlgOpenFile.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Return
''//do some stuff with dlgOpenFile.Filename
If dlgOpenFile.WasAWebResource Then
Dim finfo = New IO.FileInfo(dlgOpenFile.Filename)
finfo.Delete()
End If
In this example, I've imagined a property to dlgOpenFile "WasAWebResource" that would tell me if the file was downloaded or originally local. If it's the first case, I'll delete it.
There's no obvious way to do this, but as a workaround, how about checking where the file lives? It looks like by default this dialog downloads files to the users Temporary Internet Files directory, so you could introduce some code that looks something like this:
FileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string temporaryInternetFilesDir = Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache);
if (!string.IsNullOrEmpty(temporaryInternetFilesDir) &&
dialog.FileName.StartsWith(temporaryInternetFilesDir, StringComparison.InvariantCultureIgnoreCase))
{
// the file is in the Temporary Internet Files directory, very good chance it has been downloaded...
}
}

C# WPF - Only allow certain file extensions

I have an OpenFileDialog and I only want to allow .txt as a valid file for the users.
I know I can add a Filter to the OpenFileDialog like so:
var dialog = new OpenFileDialog();
dialog.DefaultExt = ".txt";
dialog.Filter = "Text Files (*.txt)|*.txt";
var result = dialog.ShowDialog();
// Do something with the result
The problem however, is that I can still directly say something like "test.jpg" in the OpenFileDialog and then it opens this uploads this .jpg file. (Obviously it goes wrong somewhere later, but that doesn't matter for now.) I just want to know how I can restrict the user to only add ".txt" files, nothing else? (By directly validation it inside the OpenFileDialog, instead of doing it somewhere later.)
You cant do that only in OpenFileDialog and even if you could its a bad limitation.
Using the *.txt example there are multiple files extensions that are plain text inside, *.bat or all the codding file extensions *.cs, *.js, etc...
You should not limit the user on what file he can put on it.
For more complex file types if your program cant handle the file passed by the user you should show an error not prevent the user from passing the file.

Can I use a OpenFileDialog to open all files on my harddrive

I'm learning C and C#, this question is for C#. I have a app that I want to open files on my C:\ drive. Can I use OpenFileDialog to open any type of file? How is this done?
Here is the code:
case 1:
openSomething();
break;
private static void openSomething()
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "All Files (*.*)|*.*";
open.ShowDialog();
if (open.ShowDialog() == DialogResult.OK)
{
File.Open(open.FileName); // I want to do something like this
}
}
Is there something like System.Diagnostics.Process.Start on programs for files, so I use openfiledialog to get the filename and then my code opens the file with the default application?
Edit: I answered my own question
private static void openSomething()
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "All Files (*.*)|*.*";
if (open.ShowDialog() == DialogResult.OK)
{
System.Diagnostics.Process.Start(open.FileName);
}
}
Usually the program tries to limit the kind of files listed by OpenFileDialog using the Filter property, but, if you want to allow the opening of any kind of file you set this property (before calling ShowDialog) with something like this
open.Filter = "All Files (*.*)|*.*";
meaning that every kind of file could be choosen by your user.
Keep in mind that the Filter property is just a facility to give a rapid choice to the end user, by itself the OpenFileDialog can open any kind of files also if you set any specialized filter.
All that your user is required to do is typing the *.* in the filename textbox space and he/she can choose any kind of file (of course only if he/she has the required file/folder access permissions)
After the ShowDialog returns DialogResult.OK you could check if there is something selected in the Filename property, and, if you want to open the file using the default application associated for the file extension then you use Process.Start
if (open.ShowDialog() == DialogResult.OK)
{
if(open.FileName.Trim() != string.Empty)
{
Process.Start(open.FileName);
}
}
Of course this could be problematic if the file choosen has no default program associated (for example what if the user choose a DLL?), so perhaps it is better to apply a filter to select only a subset of well known file types. But this depends on your requirements.
The OpenFileDialog really only serves as a window to let the user select a file or multiple files. It does not open any file. The selected filenames can be retrieved from the FileName or FileNames properties after dialog has been closed. To open and read / edit the file is an independent task.

Where are paths previously used in the program stored (e.g. FileDialog paths)?

According to this msdn article one of the sources for InitialDirectory property used in FileDialog is:
A path that was previously used in the program, perhaps retained from the last directory or file operation.
...
So if you selected your first file from folder x, the next time you try to select a file it will open up the FileDialog with with folder x selected (saving you having to navigate there).
Playing around with notepad this seems to carry across opening a file, saving a file, opening a file and even when printing with "Microsoft XPS Document Writer" which brings up it's own dialog.
So my question is where is this value stored between dialogs? I would like to be able to see what it is and potentially change it? The specific area i would like to change it is in the "Microsoft XPS Document Writer" printer which brings up it's own dialog. So it's not as simple as just setting the initalDirectory Value.
It's stored in the registry, somewhere in HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\ (LastVisitedPidlMRU).
You should take a look at this link:
MRU locations are what you are looking at!
Here's a way for accessing it:
var openFileDialog1 = new OpenFileDialog();
string path = openFileDialog1.InitialDirectory;
// you can change path if you want
openFileDialog1.InitialDirectory = path;
// after you are donw you can display you dialog
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// do something
}
Another way is to use Directory.SetCurrentDirectory method which sets the application's current working directory
And from Microsoft website, it is stored at this location in the registry:
//The MRU lists for Windows Explorer-style dialog boxes are stored by file type for each user in the following registry key:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU
hope this helps

Categories