开发者

Restrict if not a proper file using OpenFileDialog

开发者 https://www.devze.com 2023-01-08 02:37 出处:网络
Hi all i have written a small code to open a particular text file. Now on open file dialog under the file name a drop down is displaying some file names if i select a file from that and click on ok i

Hi all i have written a small code to open a particular text file. Now on open file dialog under the file name a drop down is displaying some file names if i select a file from that and click on ok i would like to display an error message as wrong file selected.

My code to open particular text file is as follows

openFileDialog1.FileName = string.Empty;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH";
openFileDialog1.InitialDirectory = strFilePath;
openFileDialog1.Filter = "(*.txt)|FileHeader*.txt";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    try
    {
        if ((myStream = openFileDialog1.OpenFile()) != null)
        {
            using (myStream)
            {
    开发者_JS百科            // Insert code to read the stream here.
                //FileName = openFileDialog1.FileName;
                txtFileHeader.Text = openFileDialog1.FileName.ToString(); ;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
    }

}

If any modifications to be done please let me know.


I just write my code as follows as i did not find any declaration like var

            string compareType = StringComparison.InvariantCultureIgnoreCase;
            string fileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
            string extension = Path.GetExtension(openFileDialog1.FileName);
            if (fileName.StartsWith("FileHeader", compareType)
                && extension.Equals(".txt", compareType))

I am Getting the errors as

Cannot implicitly convert type 'System.StringComparison' to 'string'

Error 3 The best overloaded method match for 'string.StartsWith(string, System.StringComparison)' has some invalid arguments

Error 4 Argument '2': cannot convert from 'string' to 'System.StringComparison'

Error 5 Static member 'string.Equals(string, string)' cannot be accessed with an instance reference; qualify it with a type name instead

So can any one please what to do...


How about something like this? You could also use regex, but I don't have VS on this machine and felt like being lazy.

I'd add, while you could file off the incorrect file name. It may be better to add an All Files (*.*)|*.* to file filter list and allow the user to select anything he might like. Then allow the application to fail during the file read if they select the wrong file. This would be more of an expected use case that would match how most other applications function.

Stream myStream;
openFileDialog1.FileName = string.Empty; 
openFileDialog1.InitialDirectory = @"..\..\ACH"; 
openFileDialog1.Filter = "FileHeader (FileHeader*.txt)|FileHeader*.txt"; 
if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    var compareType = StringComparison.InvariantCultureIgnoreCase;
    var fileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
    var extension = Path.GetExtension(openFileDialog1.FileName);
    if (fileName.StartsWith("FileHeader", compareType)
        && extension.Equals(".txt", compareType))
    {
        try 
        { 
            using (myStream = openFileDialog1.OpenFile()) 
            { 
                // Insert code to read the stream here. 
                //FileName = openFileDialog1.FileName; 
                txtFileHeader.Text = openFileDialog1.FileName;
            } 
        } 
        catch (Exception ex) 
        { 
            MessageBox.Show("Error: Could not read file from disk. " +
                            "Original error: " + ex.Message); 
        } 
    }
    else 
    {
        MessageBox.Show("Invaild File Type Selected");
    } 
} 


Final Answer and the correct one

         if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            StringComparison compareType = StringComparison.InvariantCultureIgnoreCase;
            string fileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
            string extension = Path.GetExtension(openFileDialog1.FileName);
            if (fileName.StartsWith("CCD_BatchHeader", compareType) || fileName.StartsWith("PPD_BatchHeader", compareType)
                && extension.Equals(".txt", compareType))
                try
                {

                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {

                            txtBatch.Text = openFileDialog1.FileName.ToString();
                            myStream.Close();
                        }
                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            else
            {
                MessageBox.Show("InvalidFile");
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号