Click here to Skip to main content
15,909,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter = "All Files (*.DAT)|*.DAT";
choofdlog.FilterIndex = 1;
choofdlog.Multiselect = true;
string sFileName;

if (choofdlog.ShowDialog() == DialogResult.OK)
{
sFileName = choofdlog.FileName;

}
FileStream fs = new FileStream(sFileName, FileMode.Open, FileAccess.Read);

this give error if I put sFileName this,this way "sFileName"
it pass current folder name

Please help me

Thanks in advance


Ajith

What I have tried:

this give error if I put sFileName this,this way "sFileName"
it pass current folder name
Posted
Updated 10-May-17 4:43am
Comments
[no name] 10-May-17 10:43am    
Out of the 3453463576456756 errors you can possibly get, how are we to know which one you chose?

Why would you pass an empty string to the FileStream constructor if the user clicks "Cancel"?
Richard MacCutchan 10-May-17 11:47am    
Of course it gives an error. sFileName is a variable not the name of a file. Also, why are you setting Multiselect = true when you just want to open a single file?

1 solution

Try this:
C#
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter = "All Files (*.DAT)|*.DAT";
choofdlog.FilterIndex = 1;
choofdlog.Multiselect = true;
    if (choofdlog.ShowDialog() == DialogResult.OK)
    {
    foreach (string sFileName = choofdlog.FileNames)
        {
        using (FileStream fs = new FileStream(sFileName, FileMode.Open, FileAccess.Read))
        {
            {
               ...
            }
        }
    }
When you use multiselect, you should work from the FileNames collection, not a single FileName - and you shouldn't be trying to open files if the user presses "Cancel" in the dialog.
 
Share this answer
 
v2
Comments
Member 12931315 10-May-17 11:59am    
Hi OriginalGriff1

your solution works, thank you very much and also
thanks for the other guys tried to help.

Thing is I am totally new C# and there will be many more
some what stupid problem from me.

Thanks again

Ajith (from Sri Lanka )
OriginalGriff 10-May-17 12:28pm    
You're welcome!
Member 12931315 11-May-17 5:46am    
Hi Griff

I have problem, I want to see if record exists before I write new one
I convert text file that you helped me and loaded a line to variable
and tried this

cToken1 = str.ToString().Substring(0, 9).Trim();
cToken2 = '5' + cToken1.PadLeft(5, '0');
cToken = cToken2.Substring(0, 6);
cDate = str.ToString().Substring(10, 10);
cTime = str.ToString().Substring(21, 2) + str.ToString().Substring(24, 2);
cIdno = str.ToString().Substring(30, 1);
datetime = Convert.ToDateTime(cDate);

DataRow[] foundRows;
foundRows = this.timeattDataSet.Tables["Data"].Select("Token_no= @cToken and Date = @datetime and Time = @cTime");

it gives error for @cToken ,it's say it's column, not a variable

Can you please help

Thanks in advance

Ajith
OriginalGriff 11-May-17 5:51am    
Different subject altogether, needs to be a new QA question!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900