Answered about OpenFileDialog problem

  • Wednesday, March 07, 2012 3:57 AM
     
     

    Hi:

    I meet a problem when using OpenFileDialog. when I writing a function to call it, and choose the file. And then , if I try to 

    write data and save to another file, the operation will fail. It will pass the compiler, but, will not create any file 

    if I perform the function. Is there any solution to solve the problem, thanks.

All Replies

  • Wednesday, March 07, 2012 4:44 AM
     
     Answered Has Code

    Hi,

    Check out below sample

    Create a test.txt file and select it using filedialog. and write below code

    OpenFileDialog ofd = new OpenFileDialog();
                string text=string.Empty;
                if (ofd.ShowDialog() == true)
                {
                    string filePath = ofd.FileName;
                    string safeFilePath = ofd.SafeFileName;
                    StreamReader streamReader = new StreamReader(filePath);
                    text = streamReader.ReadToEnd();
                    streamReader.Close();
                }
                text = text + " Rajwadi";
                using (StreamWriter outfile =  new StreamWriter(@"E:\AllTxtFiles.txt"))
                {
                    outfile.Write(text);
                }

    Thanks,

    Rajnikant

    • Proposed As Answer by VallarasuS Wednesday, March 07, 2012 10:28 AM
    • Marked As Answer by Kee PoppyModerator Tuesday, March 13, 2012 4:13 AM
    •  
  • Wednesday, March 07, 2012 6:39 AM
     
     
    Thanks a lot.