Save file dialog - c#

Having trouble getting a save file dialog to work with my program. I am using visual studio 2012. The save dialog opens and I can put in a file name but it won't actually save.
try
{
//declare a stream writer variables
StreamWriter outputfile;
//create a file and get a streamwriter object
outputfile = File.CreateText("organisms.txt");
//write the data to the file
for (int b = 0; b < listBox1.Items.Count; b++)
{
outputfile.WriteLine(listBox1.Items[b].ToString());
}
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("You clicked the save button");
}
else
{
MessageBox.Show("You hit the cancel button");
}
//close the file
outputfile.Close();
}

I'm going to use pseudo code for this one. The code you're showing is saying that you're making a file called organisms.txt. And um, you're not really using the SaveDialog.
The way you've coded it is that, regardless of whatever the user chose - whether to save it or not - the program will end up writing to organisms.txt.
You need to change the following;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("You clicked the save button");
}
Just cut and paste the loop and the stream there. Change organisms.txt with saveFileDialof1.FileName so that it saves it wherever the Save Dialog points to. Although I don't really truly understand StreamWriter that much, so, do help me tweak the code.
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("You clicked the save button");
//create a file and get a streamwriter object
outputfile = File.CreateText(saveFileDialog1.FileName);
//write the data to the file
for (int b = 0; b < listBox1.Items.Count; b++)
{
outputfile.WriteLine(listBox1.Items[b].ToString());
}
}

StreamWriter implements IDisposable.
Proper usage would be:
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("You clicked the save button");
using (StreamWriter outputfile = File.CreateText("organisms.txt"))
{
for (int b = 0; b < listBox1.Items.Count; b++)
{
outputfile.WriteLine(listBox1.Items[b].ToString());
}
}
}
else
{
MessageBox.Show("You hit the cancel button");
}
If that still doesn't work:
1) See if any exceptions are being thrown
2) Keep in mind that you are not using the file from the dialog, but 'organisms.txt' which would be saved in the working directory of the app

Related

C# Try/Catch Skips Nested If/Else Statement and Wont Open SaveDialog

I am working on a school project but I cant figure out this last bug. It is supposed to open a saveFileDialog when the first if statement returns false. But instead of continuing on into the else statement, it goes straight to throwing the exception and never opens the saveFile Dialog. It gives me the following error: Code: The path is not of a legal form.
I dont understand what the problem is. The user should be able to select the path in the save dialog that pops up. The file doesnt exist yet and its supposed to open the save file dialog to make the file.
private void btnSave_Click(object sender, EventArgs e)
{
// Declare StreamWriter object
StreamWriter outputFile;
// Try to write file
try
{
// If current file is > 0
if (new FileInfo(currentFile).Length > 0)
{
// Create output file using current file
outputFile = File.CreateText(currentFile);
// Loop through current file and write lines to output file
for (int i = 0; i < lstBoxLog.Items.Count; i++)
{
outputFile.WriteLine(lstBoxLog.Items[i].ToString());
}
// Close text file
outputFile.Close();
}
// Else open save dialog for user to save file
else
{
// If save file dialog is equal to dialog result
if (saveFile.ShowDialog() == DialogResult.OK)
{
// Open output file object with create text
outputFile = File.CreateText(saveFile.FileName);
// Set currentFile to = savefile dialog
currentFile = saveFile.FileName;
// Loop through each line and write to file
for (int i = 0; i < lstBoxLog.Items.Count; i++)
{
outputFile.WriteLine(lstBoxLog.Items[i].ToString());
}
// Close text file
outputFile.Close();
}
// Else show error message
else
{
// Display message box dialog
MessageBox.Show("Cannot save file.", "Not Saved");
}
}
}
// Display error message.
catch (Exception ex)
{
// Display message box dialog
MessageBox.Show("Save canceled. \n\nCode: " + ex.Message, "Save Error!");
}
}
try
{
if (File.Exists(currentFile))
{
if (new FileInfo(currentFile).Length > 0)
{
...
}
}
else
{
//show save file dialog
}
}
catch
{
...
}
Per Rob's suggestion this is what I used.
try
            {
                // If current file is > 0
                if (currentFile.Length > 0)
                {
                   // Create output file using current file
                   outputFile = File.CreateText(currentFile);

Why does my SaveFileDialog display again if cancelled?

I have a SaveFileDialog in my program. The issue is that when I click "Cancel" on the dialog, another SaveFileDialog opens up. But when I click cancel on the second SaveFileDialog, a third does NOT appear, so it's not a loop or anything like that. I can't see what is causing my SaveFileDialog to behave in such an odd manner. Obviously I need to fix this so that if the user clicks cancel on the first SaveFileDialog, it returns them to the form.
The code for saving in my program is as follows:
private void SaveFile()
{
if (filepath == null)
{
SaveFileAs();
}
else
{
StreamWriter sw = new StreamWriter(filepath);
try
{
sw.WriteLine(richTextBoxPrintCtrl1.Rtf);
richTextBoxPrintCtrl1.Modified = false;
sw.Close();
lastsave.Text = "Last Saved: " + DateTime.Now.ToString();
}
catch (Exception exc)
{
MessageBox.Show("Failed to save file. \n \n" + exc.Message);
}
finally
{
if (sw != null) sw.Close();
}
And SaveFileAs
private void SaveFileAs()
{
SaveFileDialog sfdSaveFile = new SaveFileDialog();//Creates a new instance of the SaveFileDialog
sfdSaveFile.Title = "Save File";//The title of the SaveFileDialog window
sfdSaveFile.FileName = "Untitled";//The default filename in the SaveFileDialog window
sfdSaveFile.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt";//The supported file extensions when saving
if (sfdSaveFile.ShowDialog() == DialogResult.OK)//If the condition is correct, run the lines of code
try//try to run the code
{
filepath = sfdSaveFile.FileName;//get the filepath of the file once it is saved
SaveFile();//Calls the SaveFile object
this.Text = string.Format("{0} - Basic Word Processor", Path.GetFileName(sfdSaveFile.FileName));//Set the form name
lastsave.Text = "Last Saved: " + DateTime.Now.ToString();//Writes the text to the lastsave.Text label, followed by the current date and time
richTextBoxPrintCtrl1.Modified = false;
return;
}
catch (Exception exc)//Catches any errors
{
MessageBox.Show("An error occured whilst saving. " + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (sfdSaveFile.ShowDialog() == DialogResult.Cancel)
{
return;
}
else if (sfdSaveFile.ShowDialog() == DialogResult.Cancel)//If the condition is true, run the line of code
{
return;
}
If anyone could help me determine why this is occurring, I'd really appreciate it..
--EDIT--
I forgot to mention that if the user does go through and save the file, the SaveFileDialog doesn't open up another SaveFileDialog. It is something to do with cancelling the SaveFileDialog which causes the issue.
sfdSaveFile.ShowDialog() opens the file dialog. If it's not DialogResult.OK the first time, it goes to the else clause and gets called again. Store the result of ShowDialog and check what it is, don't call it every time.
In order to do so, use this sort of if/else:
DialogResult dialogResult = sfdSaveFile.ShowDialog();
if (dialogResult == DialogResult.OK)
{
}
else if (dialogResult == DialogResult.Cancel)
{
}

How do I make it so that when the user clicks cancel, it cancels the dialog?

I have the following code:
Open File Code
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open File";
ofd.FileName = "";
ofd.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html"; StreamReader sr = null;
if (ofd.ShowDialog() != DialogResult.Yes) return;
{
NewFile();
}
try
{
sr = new StreamReader(ofd.FileName);
this.Text = string.Format("{0} - Basic Word Processor", Path.GetFileName(ofd.FileName));
richTextBoxPrintCtrl1.Text = ofd.FileName;
richTextBoxPrintCtrl1.Text = sr.ReadToEnd();
filepath = ofd.FileName;
richTextBoxPrintCtrl1.LoadFile(fileName, RichTextBoxStreamType.RichText);
}
catch
{
}
finally
{
if (sr != null) sr.Close();
}
New File Code
if (richTextBoxPrintCtrl1.Modified)
{
DialogResult r = MessageBox.Show(this, "Save Current Document?", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (r == DialogResult.Yes) SaveFile();
if (r == DialogResult.Cancel) return;
}
this.Text = string.Format("Untitled - Basic Word Processor");
richTextBoxPrintCtrl1.Text = "";
filepath = null;
}
}
SaveFileAs Code
SaveFileDialog sfdSaveFile = new SaveFileDialog();
sfdSaveFile.Title = "Save File";
sfdSaveFile.FileName = "Untitled";
sfdSaveFile.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html";
if (sfdSaveFile.ShowDialog() == DialogResult.OK)
try
{
filepath = sfdSaveFile.FileName;
SaveFile();
this.Text = string.Format("{0} - Basic Word Processor", Path.GetFileName(sfdSaveFile.FileName));
}
catch (Exception exc)
{
}
SaveFile Code
if (filepath == null)
{
SaveFileAs();
return;
}
StreamWriter sw = new StreamWriter(filepath);
//StreamWriter stwrite = null;
try
{
sw.WriteLine(richTextBoxPrintCtrl1.Text);
richTextBoxPrintCtrl1.Modified = false;
sw.Close();
}
catch (Exception e)
{
MessageBox.Show("Failed to save file. \n" + e.Message);
}
finally
{
if (sw != null) sw.Close();
}
Currently, the program skips the NewFile event (even if the text has been modified). How can I make it so that when I click "Open", it asks me if I would like to save (if the text is modified). Then if I click cancel, it returns me to the form?
Sorry. I'm really new to programming so this is all a learning curve.
Okay, I think I see what's going on here. First off I don't believe return; works the way you think it does.
if (ofd.ShowDialog() != DialogResult.Yes) return;
{
NewFile();
}
You have a return; call that happens if the show dialog is not yes. The { newFile() } code doesn't need braces around it. So those lines are really:
if (ofd.ShowDialog() != DialogResult.Yes) return;
NewFile();
Now, given your requirement, NewFile is called WAY too late in the game anyway. You want that to happen before you ask them what to open; just like most other windows programs work.
But, there's another issue. Your return statement in the NewFile method is simply returning from NewFile. It's not telling the previous method to bail out.
So the NewFile method needs a return type to indicate whether to allow the calling method to go forward or not.
And, looking at your save file you have a return method there too. What's with all of the return; calls?
Which brings us back to how do we fix this?
Answer: rewrite the whole thing. Starting with the following method:
private Boolean CanClear() {
Boolean result = false;
if (richTextBoxPrintCtrl1.Modified)
{
DialogResult r = MessageBox.Show(this, "Save Current Document?", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (r == DialogResult.Yes) {
SaveFile();
result = true;
}
} else {
result = true;
}
return result;
}
Now, in your Open and New file methods do the following (assuming these are the method headers)
protected void OpenFile(...) {
if (!CanClear()) return;
.... now execute the code to load the open dialog and the selected file.
}
protected void NewFile(...) {
if (!CanClear()) return;
this.Text = string.Format("Untitled - Basic Word Processor");
richTextBoxPrintCtrl1.Text = "";
filepath = null;
}
The problem is here:
if (ofd.ShowDialog() != DialogResult.Yes) return;
{
NewFile();
}
remove that return. But, as #Chris says, you should ask whether to save the current file or not before the user selects the new file to open.

Update ui thread

I have a ListBox which I put some files, if the file is not AVI I automatically converts it but I want when the files converting message will write on a label that the files are now converted to another format, i know i need use Dispatcher in order to update the UI thread but i use now Winform instead of WPF, and i need help with this.
BTW i cannot use Task because i am using .Net 3.5
private void btnAdd_Click(object sender, EventArgs e)
{
System.IO.Stream myStream;
OpenFileDialog thisDialog = new OpenFileDialog();
thisDialog.InitialDirectory = "c:\\";
thisDialog.Filter = "All files (*.*)|*.*";
thisDialog.FilterIndex = 1;
thisDialog.RestoreDirectory = false;
thisDialog.Multiselect = true; // Allow the user to select multiple files
thisDialog.Title = "Please Select Source File";
thisDialog.FileName = lastPath;
List<string> list = new List<string>();
if (thisDialog.ShowDialog() == DialogResult.OK)
{
foreach (String file in thisDialog.FileNames)
{
try
{
if ((myStream = thisDialog.OpenFile()) != null)
{
using (myStream)
{
listBoxFiles.Items.Add(file);
lastPath = file;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
for (int i = 0; i < listBoxFiles.Items.Count; i++)
{
string path = (string)listBoxFiles.Items[i];
FileInfo fileInfo = new FileInfo(path);
if (fileInfo.Extension != ".AVI")
{
listToRemove.Add(path);
}
}
(new System.Threading.Thread(sendFilesToConvertToPcap)).Start();
foreach (string file in listToRemove) //remove all non .AVI files from listbox
{
listBoxFiles.Items.Remove(file);
}
}
}
this function need to change the Label:
public void sendFilesToConvertToPcap()
{
if (listToRemove.Count == 0) // nothing to do
{
return;
}
lblStatus2.Content = "Convert file to .AVI...";
foreach (String file in listToRemove)
{
FileInfo fileInfo = new FileInfo(file);
myClass = new (class who convert the files)(fileInfo);
String newFileName = myClass.mNewFileName;
listBoxFiles.Items.Add(myClass._newFileName);
}
lblStatus2.Content = "Finished...";
}
From your question, it seems that you'd like to convert several files. You may want to consider using the BackgroundWorker class and overwrite the DoWork and ProgressChanged events as described in this article. You can update the label and other controls in the ProgressChanged event.
public void sendFilesToConvertToPcap()
{
.....
....
this.Invoke((MethodInvoker)delegate {
lblStatus2.Text = "Convert file to .AVI..."; });
....
}
This is a very common requirement for long-running processes. If you don't explicitly call methods on a separate thread, they will automatically run on the main (see: UI) thread and cause your UI to hand (as I suspect you already know).
http://www.dotnetperls.com/backgroundworker
Here is an old, but excellent link with an example on how to use the background worker to handle the threadpool for you.
This way, you can just create a worker to manage running your conversion process on a separate thread, and I believe there is even an example for creating a process bar.

c# Unable to open file for reading

I'm writing a program that uses FileSystemWatcher to monitor changes to a given directory, and when it receives OnCreated or OnChanged event, it copies those created/changed files to a specified directories. At first I had problems with the fact that OnChanged/OnCreated events can be sent twice (not acceptable in case it needed to process 500MB file) but I made a way around this and with what I'm REALLY BLOCKED with is getting the following IOException:
The process cannot access the file 'C:\Where are Photos\bookmarks (11).html' because it is being used by another process.
Thus, preventing the program from copying all the files it should.
So as I mentioned, when user uses this program he/she specifes monitored directory, when user copies/creates/changes file in that directory, program should get OnCreated/OnChanged event and then copy that file to few other directories.
Above error happens in all cases, if user copies few files that needs to overwrite other ones in folder being monitored or when copying bulk of several files or even sometimes when copying one file in a monitored directory.
Whole program is quite big so I'm sending the most important parts.
OnCreated:
private void OnCreated(object source, FileSystemEventArgs e) {
AddLogEntry(e.FullPath, "created", "");
// Update last access data if it's file so the same file doesn't
// get processed twice because of sending another event.
if (fileType(e.FullPath) == 2) {
lastPath = e.FullPath;
lastTime = DateTime.Now;
}
// serves no purpose now, it will be remove soon
string fileName = GetFileName(e.FullPath);
// copies file from source to few other directories
Copy(e.FullPath, fileName);
Console.WriteLine("OnCreated: " + e.FullPath);
}
OnChanged:
private void OnChanged(object source, FileSystemEventArgs e) {
// is it directory
if (fileType(e.FullPath) == 1)
return; // don't mind directory changes itself
// Only if enough time has passed or if it's some other file
// because two events can be generated
int timeDiff = ((TimeSpan)(DateTime.Now - lastTime)).Seconds;
if ((timeDiff < minSecsDiff) && (e.FullPath.Equals(lastPath))) {
Console.WriteLine("-- skipped -- {0}, timediff: {1}", e.FullPath, timeDiff);
return;
}
// Update last access data for above to work
lastPath = e.FullPath;
lastTime = DateTime.Now;
// Only if size is changed, the rest will handle other handlers
if (e.ChangeType == WatcherChangeTypes.Changed) {
AddLogEntry(e.FullPath, "changed", "");
string fileName = GetFileName(e.FullPath);
Copy(e.FullPath, fileName);
Console.WriteLine("OnChanged: " + e.FullPath);
}
}
fileType:
private int fileType(string path) {
if (Directory.Exists(path))
return 1; // directory
else if (File.Exists(path))
return 2; // file
else
return 0;
}
Copy:
private void Copy(string srcPath, string fileName) {
foreach (string dstDirectoy in paths) {
string eventType = "copied";
string error = "noerror";
string path = "";
string dirPortion = "";
// in case directory needs to be made
if (srcPath.Length > fsw.Path.Length) {
path = srcPath.Substring(fsw.Path.Length,
srcPath.Length - fsw.Path.Length);
int pos = path.LastIndexOf('\\');
if (pos != -1)
dirPortion = path.Substring(0, pos);
}
if (fileType(srcPath) == 1) {
try {
Directory.CreateDirectory(dstDirectoy + path);
//Directory.CreateDirectory(dstDirectoy + fileName);
eventType = "created";
} catch (IOException e) {
eventType = "error";
error = e.Message;
}
} else {
try {
if (!overwriteFile && File.Exists(dstDirectoy + path))
continue;
// create new dir anyway even if it exists just to be sure
Directory.CreateDirectory(dstDirectoy + dirPortion);
// copy file from where event occured to all specified directories
using (FileStream fsin = new FileStream(srcPath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
using (FileStream fsout = new FileStream(dstDirectoy + path, FileMode.Create, FileAccess.Write)) {
byte[] buffer = new byte[32768];
int bytesRead = -1;
while ((bytesRead = fsin.Read(buffer, 0, buffer.Length)) > 0)
fsout.Write(buffer, 0, bytesRead);
}
}
} catch (Exception e) {
if ((e is IOException) && (overwriteFile == false)) {
eventType = "skipped";
} else {
eventType = "error";
error = e.Message;
// attempt to find and kill the process locking the file.
// failed, miserably
System.Diagnostics.Process tool = new System.Diagnostics.Process();
tool.StartInfo.FileName = "handle.exe";
tool.StartInfo.Arguments = "\"" + srcPath + "\"";
tool.StartInfo.UseShellExecute = false;
tool.StartInfo.RedirectStandardOutput = true;
tool.Start();
tool.WaitForExit();
string outputTool = tool.StandardOutput.ReadToEnd();
string matchPattern = #"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";
foreach (Match match in Regex.Matches(outputTool, matchPattern)) {
System.Diagnostics.Process.GetProcessById(int.Parse(match.Value)).Kill();
}
Console.WriteLine("ERROR: {0}: [ {1} ]", e.Message, srcPath);
}
}
}
AddLogEntry(dstDirectoy + path, eventType, error);
}
}
I checked everywhere in my program and whenever I use some file I use it in using block so even writing event to log (class for what I omitted since there is probably too much code already in post) wont lock the file, that is it shouldn't since all operations are using using statement block.
I simply have no clue who's locking the file if not my program "copy" process from user through Windows or something else.
Right now I have two possible "solutions" (I can't say they are clean solutions since they are hacks and as such not desirable). Since probably the problem is with fileType method (what else could lock the file?) I tried changing it to this, to simulate "blocking-until-ready-to-open" operation:
fileType:
private int fileType(string path) {
FileStream fs = null;
int ret = 0;
bool run = true;
if (Directory.Exists(path))
ret = 1;
else {
while (run) {
try {
fs = new FileStream(path, FileMode.Open);
ret = 2;
run = false;
} catch (IOException) {
} finally {
if (fs != null) {
fs.Close();
fs.Dispose();
}
}
}
}
return ret;
}
This is working as much as I could tell (test), but... it's hack, not to mention other deficients.
The other "solution" I could try (I didn't test it yet) is using GC.Collect() somewhere at the end of fileType() method. Maybe even worse "solution" than previous one.
Can someone pleas tell me, what on earth is locking the file, preventing it from opening and how can I fix that? What am I missing to see?
Thanks in advance.
The problem is most likely that the file is still being copied while you already try to access it. This can happen especially on large files.
You can try to check whether the file can be opened with write permissions before you actually start your processing. For details how to do that check here.
If you can influence the process creating the file there might be a better solution. First copy the file with a temporary extension, and then, after the copying is completed, rename it so that the FileSystemWatcher event will be triggered.
You can try with Volume Shadow Copies.
See www.codeproject.com/KB/dotnet/makeshadowcopy.aspx for more details.
FileSystemWatcher events trigger when the file begins the copy, not at the end, so it's common to run into this kind of errors.
Your first approach will work, however, I would recommend spinning the I/O intensive code on another thread, and using an incremental Sleep() instead of the busy waiting you do.
However, if you have access to the software that actually creates the files, the extension changing is a slightly less complicated solution. Just beware, that a xls filter on the FileSystemwatcher will match a file called myfile1.xls.temp, as I found that out the hard way :)

Categories