0

This is similar to older posts on this site but I keep getting an error message. I want to create a button in C # WPF that opens a dialogbox and saves a text file to be read at a later date. This code works for windows 32, but crashes on windows 64. How can I change this code to get it to work on both systems? I am a beginner at programming.

Microsoft.Win32.SaveFileDialog saveFile = new Microsoft.Win32.SaveFileDialog(); //throws error message here

    private void savebutton_Click(object sender, RoutedEventArgs e)
    {
        saveFile.FileName = Class1.stringjobnum; 
        saveFile.Filter = "CCurtain (*.cur)|*.cur"; 
        saveFile.FilterIndex = 2; 
        saveFile.InitialDirectory = "T:\\Tank Baffle Curtain Calculator\\SavedTanks"; 
        saveFile.OverwritePrompt = true; 

        bool? result = saveFile.ShowDialog();

        if (result.HasValue && result.Value)
        {
            clsSaveFile.s_FilePath = saveFile.FileName;
            int iDotLoc = clsSaveFile.s_FilePath.LastIndexOf('.');


            string strExtTest = clsSaveFile.s_FilePath.Substring(iDotLoc);
            if (strExtTest != ".cur")
                clsSaveFile.s_FilePath += ".cur";
            FileInfo sourceFile = new FileInfo(clsSaveFile.s_FilePath);
            clsSaveFile.saveFile();
        }
    }
  • 1
    Is there some reason you're using the Microsoft.Win32 one instead of the System.Windows.Forms one? – djdanlib Dec 28 '11 at 21:00
  • What is the error message that you get? – decyclone Dec 28 '11 at 21:07
  • Getting SaveFileDialog to crash at the constructor indicates your machine is pretty borked. Post to superuser.com to ask about ways to get it stable again. – Hans Passant Dec 28 '11 at 21:41
  • I have attempted to use the System.Windows.Forms one and it keeps giving me things about missing references. I couldn't figure that out and so I went with this code because it worked with my machine. (but not others). I had a hunch the win32 is the underlying issue. If you can tell me what references to add I can try that instead. – user1119958 Dec 28 '11 at 22:12
  • I got it now. You were right djdanlib, you have to use windows forms; here is the code that works: I added SaveFileDialog saveFile = new SaveFileDialog(); inside the private void and took out the line starting with Microsoft.Win32..... – user1119958 Dec 29 '11 at 15:44

3 Answers3

0

You're setting an invalid FilterIndex, that might have something to do with it.

There is no 2nd filter in the filter string as written:

"CCurtain (*.cur)|*.cur"

Try setting the FilterIndex to 1 or adding another filter to the string.

djdanlib
  • 21,449
  • 1
  • 20
  • 29
0

You should try adding a catch around the statement to get a better idea as to what is going on.

try
{ 
    code here
}
catch (Exception ex)
{
   ex.message contains the info
}

Also, check for null:

bool? result = saveFile.ShowDialog();

if (result != null &&  (result.HasValue && result.Value))
{
// code
}
JeremyK
  • 1,075
  • 1
  • 22
  • 45
  • 1
    I did what you said and it has helped somewhat. I now know that it is failing somewhere inside that IF statement. I wasn’t able to break it down further. – user1119958 Dec 28 '11 at 21:38
  • Which if statement of the 2? And can you post the exception message? – JeremyK Dec 28 '11 at 21:46
  • Keep in mind, if the null-able bool is set to null, your if statement will crash is it checks members of the bool?. Try checking for null first, if null don't do anything else. I updated my answer. – JeremyK Dec 28 '11 at 21:47
  • the first if statement was the one I was referring to. My value for result does not seem to be null. – user1119958 Dec 28 '11 at 22:07
0

I would create the dialogbox IN the event. And you don't have two different filters.

    private void savebutton_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.SaveFileDialog saveFile = new Microsoft.Win32.SaveFileDialog();
        saveFile.FileName = Class1.stringjobnum; 
        saveFile.Filter = "CCurtain|*.cur";; 
        saveFile.FilterIndex = 1; 
        saveFile.InitialDirectory = "T:\\Tank Baffle Curtain Calculator\\SavedTanks"; 
        saveFile.OverwritePrompt = true; 

        // Show open file dialog box
        Nullable<bool> result = saveFile.ShowDialog();

        // Process open file dialog box results
        if (result == true)
        {
            string filename = saveFile.FileName;
            // are you sure you need to check the extension.
            // if so extension is a a fileinfo property
        }
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Copy and pasted what you gave me into my code. The dlg.ShowDialog(); threw an error. I changed it to saveFile.ShowDialog();. That was probably my error. Still, I ran that same exact code as the one above (except for the dlg change) and it worked fine as well in windows32. However, when I try to run on a windows64 it still crashes. – user1119958 Dec 28 '11 at 21:50
  • 1
    I just tested it on a Windows 2008 R2 Server (64 bit) and it works. Check you compiler setting. Mine are any CPU. If the compiler is set for 64 bit then that control may not work. – paparazzo Dec 28 '11 at 22:11
  • Can you give me a link on how to change my compiler settings? – user1119958 Dec 28 '11 at 22:19
  • 1
    You just right click on the properties and select the build tab. But I changed to 64 bit and it still worked for me so I just don't know. – paparazzo Dec 28 '11 at 22:28