I am trying to download a file from a URL and I want to have a Popup where I can decide where to save the file on my pc. I know how to save it to a setlocation but that's not what I want.
WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri("URL"), #"d:\location");
So I get that this is how I download it so a set location but I need to be able to save it to a location of choice with the usual popup you normally get when you download anything.
To give more sight into this I have 2 radiobuttonlists in which the user can check what topics he wants then he can choose from a dropdownlist which file he wants to download and then he click on a downloadbutton which should trigger the download of that file.
Use DownloadFileAsync and listen to DownloadFileFinished. First download it to temp file and at event DownloadFileFinished, show popup and ask where to save. Then just copy the file from temp file to the filename from user.
-or-
Show SaveFileDialog before you start DownloadFileAsync.
Talking about a desktop app you can use SaveFileDialog.
The msdn code example:
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream ;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
myStream.Close();
}
}
}
Related
I am attempting to create a note taking application (first Windows Forms application). So far I have managed to read a .txt file into a RichTextBox. I am trying to make the program create and save the .txt file containing the contents of the .txt file that was read in read in. So when the user adds text from a file it creates a new file in a notes folders that is in the root directory of application. Please see my code below. Any advice would be greatly appreciated. Cheers
private void button1_Click(object sender, EventArgs e)
{
//read in a .txt file
OpenFileDialog op = new OpenFileDialog();
if (op.ShowDialog() == DialogResult.OK)
richTextBox1.LoadFile(op.FileName, RichTextBoxStreamType.PlainText);
this.Text = op.FileName;
string fileName = op.FileName;
//create new .txt file contaning module notes
System.IO.StreamWriter file = new System.IO.StreamWriter("\\"+fileName);
file.WriteLine(fileName);
file.Close();
}
Tested and working
//read in a .txt file
OpenFileDialog op = new OpenFileDialog();
if (op.ShowDialog() == DialogResult.OK)
richTextBox1.LoadFile(op.FileName, RichTextBoxStreamType.PlainText);
this.Text = op.FileName;
string fileName = Path.Combine(Application.CommonAppDataPath, Path.GetFileName(op.FileName));
File.WriteAllText(fileName, "test");
wrap your streams, files (anything that implements IDisposable) in a using block:
using(var myfile = File. CreateText(path) ) {
myfile.WriteLine("hi");
}
it's not creating a file because the path is wrong
I am trying to bypass the save dialog box when using the SaveFileDialog class. I want to be able to write to a document without having to prompt a user to decide if they want to save or not, the file should automatically save when they click a button.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.InitialDirectory = #"C:\";
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
// Code to write the stream goes here.
}
I have tried removing the if statement as well as using...
saveFileDialog1.CreatePRompt = false;
Nothing seems to work... Any ideas?
I think you want to bypass the overwrite prompt dialog.
In this case you can use
saveFileDialog1.OverwritePrompt = false;
Otherwise, you don't need a SaveFileDialog and you can save your stream without using it.
I found my answer. The question I asked was actually two it seems. The first was how to bypass SaveFileDialog, I wanted to use saveFileDialog because it can remember the last folder it accessed and opens to that folder when performing a read/save. That being said I implemented this...
Directory = System.AppDomain.CurrentDomain.BaseDirectory;
This will set Directory to the location of my executable.
Next I got rid of SaveFileDialog and just wrote to a file without ever prompting the user.
Thanks for all the pointers and ideas!
The end result ended up...
Directory = System.AppDomain.CurrentDomain.BaseDirectory;
using(System.IO.StreamWriter file = new System.IO.StreamWriter(Directory, false))
{
// File contents
}
Works perfectly for what I need it for.
If you want to save but you need the user to pick a filename, I would use this:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
//setup properties of Dialog
bool filenamepicked = false;
while (!filenamepicked)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//Save file
filenamepicked = true;
}
else
{
MessageBox.Show("You have to use a file name.");
}
}
I know that this is a very common problem, I tried to use processes, but it doesn't work. So, I want an user to be able to upload photos, to create its own avatar. What I do, is to take the selected photo, I resize it , than I crop it and save it in application/bin. If he chooses another photo again, I delete the first photo and I create another. After I delete the photo, I get this error:
The process cannot access the file : 'C:\.." because it is being used by another process.
Code:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
try
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Title = "Open Image";
dlg.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
if (dlg.ShowDialog() == true)
{
string fileName = dlg.FileName;
Uri uri = new Uri(fileName, UriKind.RelativeOrAbsolute);
BitmapImage bit = new BitmapImage(uri);
Bitmap b = new Bitmap(fileName);
b = ResizeImage(b, 100, 100);
b = CropCircleImage(b);
string path = System.AppDomain.CurrentDomain.BaseDirectory + #"UserPhotos\";
string file =Button.Content.ToString()+".bmp";
if (File.Exists(path + file))
File.Delete(path + file);
//after it gets out from the if statement, I get the error
b.Save(path + file);
Uri newuri = new Uri(path + file, UriKind.RelativeOrAbsolute);
((ButtonLogin)sender).Image= new BitmapImage(newuri);
This might be because you are updating the image of a particular user and you are trying to delete the old one and upload the updated one when the old one is in use. Try 'de-referencing' the old one and then upload the new one.
Sorry for the word De-referencing. By it I mean to say is to check whether that image is used somewhere else and is locked. If so, unlock it and upload the new one.
Simply put, either your process, or another process has opened the file and locked it so that your program is not able to open the file. Typically this happens when, due to a coding error, you fail to properly close the file.
The error in your code is probably not in the code that you have shown. Most likely the error is in code that you have not shown. Code that opens the file but then fails to close it.
If you cannot work out which process holds a lock on the file you can use a debugging tool like Process Monitor or Process Explorer. These tools can both show you which process holds the lock on the file.
I need to create and write to a .dat file. I'm guessing that this is pretty much the same process as writing to a .txt file, but just using a different extension.
In plain english I would like to know how to:
-Create a .dat file
-Write to it
-And save the file using SaveFileDialog
There are a few pages that I've been looking at, but I think that my best explanation will come from this site because it allows me to state exactly what I need to learn.
The following code is what I have at the moment. Basically it opens a SaveFileDialog window with a blank File: section. Mapping to a folder and pressing save does not save anything because there is no file being used. Please help me use this to save files to different locations.
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "";
dlg.DefaultExt = "";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
}
Pages that I've been looking at:
-http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx
-http://social.msdn.microsoft.com/Forums/en-US/cd0b129f-adf1-4c4f-9096-f0662772c821/how-to-use-savefiledialog-for-save-text-file
-http://msdn.microsoft.com/en-us/library/system.io.file.createtext(v=vs.110).aspx
Note that the SaveFileDialog only yields a filename but does not actually save anything.
var sfd = new SaveFileDialog {
Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*",
// Set other options depending on your needs ...
};
if (sfd.ShowDialog() == true) { // Returns a bool?, therefore the == to convert it into bool.
string filename = sfd.FileName;
// Save the file ...
}
Use the filename you are getting from the SaveFileDialog and do the following:
File.WriteAllText(filename, contents);
That's all if you intend to write text to the file.
You can also use:
File.WriteAllLines(filename, contentsAsStringArray);
using(StreamWriter writer = new StreamWriter(filename , true))
{
writer.WriteLine("whatever your text is");
}
I have a browse button in my windows form application and i wanted to only filter down to the option of choosing pdf files. So in the browse file window only pdf files will be visible and not showing .doc or any kind of document format.
private void btnSelectFile_Click(object sender, EventArgs e)
{
var dlg = new OpenFileDialog();
var res = dlg.ShowDialog();
if (res == DialogResult.OK)
{
DocumentUNCPath.Text = dlg.FileName;
}
}
Firstly you need to apply a filter first to the OpenFileDialog such as:
dlg.Filter = "PDF Files|*.pdf";
However, that doesn't stop them from forcing through a file (which they can do). You can again check the filename again after they click on OK but this is no guarantee that the file you get will be a PDF.
To be safe you could use a PDF library either locally or server side to try and open the PDF file and see if it really is such.
Add this:
dlg.Filter = "PDF files|*.pdf";
You'll want to set the filter property on your dlg object like this:
var dlg = new OpenFileDialog();
dlg.Filter = "*.pdf";
var res = dlg.ShowDialog();
if (res == DialogResult.OK)
{
DocumentUNCPath.Text = dlg.FileName;
}
You want to use the Filter property of the OpenFileDialog.
dlg.Filter = "PDF Files|*.pdf"
The portion to the left of the | can be anything, I just gave you an example, but it's what's shown to the user. The portion to the right of the | is the actual Windows filter.