folder browse and file access

  • Thread starter Thread starter Sirin Azazi
  • Start date Start date
S

Sirin Azazi

Hi,
I wrote simple program to get the .c and .h files in a folder and number of
lines they contain. It works; but when i choose the 'c:\' folder, i get an
Argument Exception.
when i choose another driver or folder, there is no error. what could be the
reason? if the reason is hidden system files which is being used by System,
is there a way to access files (for reading) as they are being used?


that is a part of the program (it's in a "try-catch(ArgumentException)"
block);

for (int i = 0; i < files.Length; i++)
{

if ((files.Substring(files.Length - 2) == ".c") ||
(files.Substring(files.Length - 2) == ".C")

|| (files.Substring(files.Length - 2) == ".h") ||
(files.Substring(files.Length - 2) == ".H"))


{

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

StreamReader reader = new StreamReader(fs);

str = reader.ReadToEnd();

int count = 0;

for (int k = 0; k < str.Length; k++)

{

if (str[k] == '\n')

count++;

}

reader.Close();

fs.Close();

fs.Dispose();

files = files.Substring(fpath.Length);

if (files.StartsWith("\\")) files = files.Substring(1);

newrow = new string[]{ files,(count+1).ToString() ,null };

dataGridView1.Rows.Add(newrow);

dataGridView1.Rows.HeaderCell.Value = i.ToString();

}

}
 
Hi,

What does the message of ArgumentException says? , where it's thrown?

IIRC this exception is thrown only when an argument is invalid, get the
line that is throwning it wrapped in a try/catch and see what values the
parameters have.

If unable to solve it just post the above answer here.
 
I think the brief exception code is;
-COMPlusExceptionCode -532459699

haber said:
Hi,

What does the message of ArgumentException says? , where it's thrown?

IIRC this exception is thrown only when an argument is invalid, get the
line that is throwning it wrapped in a try/catch and see what values the
parameters have.

If unable to solve it just post the above answer here.



--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Sirin Azazi said:
Hi,
I wrote simple program to get the .c and .h files in a folder and number
of lines they contain. It works; but when i choose the 'c:\' folder, i
get an Argument Exception.
when i choose another driver or folder, there is no error. what could be
the reason? if the reason is hidden system files which is being used by
System, is there a way to access files (for reading) as they are being
used?


that is a part of the program (it's in a "try-catch(ArgumentException)"
block);

for (int i = 0; i < files.Length; i++)
{

if ((files.Substring(files.Length - 2) == ".c") ||
(files.Substring(files.Length - 2) == ".C")

|| (files.Substring(files.Length - 2) == ".h") ||
(files.Substring(files.Length - 2) == ".H"))


{

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

StreamReader reader = new StreamReader(fs);

str = reader.ReadToEnd();

int count = 0;

for (int k = 0; k < str.Length; k++)

{

if (str[k] == '\n')

count++;

}

reader.Close();

fs.Close();

fs.Dispose();

files = files.Substring(fpath.Length);

if (files.StartsWith("\\")) files = files.Substring(1);

newrow = new string[]{ files,(count+1).ToString() ,null };

dataGridView1.Rows.Add(newrow);

dataGridView1.Rows.HeaderCell.Value = i.ToString();

}

}

 
Hi,

In what line?

What about the InnerException? Post the Message of both here.


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Sirin Azazi said:
I think the brief exception code is;
-COMPlusExceptionCode -532459699

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

What does the message of ArgumentException says? , where it's thrown?

IIRC this exception is thrown only when an argument is invalid, get the
line that is throwning it wrapped in a try/catch and see what values the
parameters have.

If unable to solve it just post the above answer here.



--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Sirin Azazi said:
Hi,
I wrote simple program to get the .c and .h files in a folder and
number of lines they contain. It works; but when i choose the 'c:\'
folder, i get an Argument Exception.
when i choose another driver or folder, there is no error. what could be
the reason? if the reason is hidden system files which is being used by
System, is there a way to access files (for reading) as they are being
used?


that is a part of the program (it's in a "try-catch(ArgumentException)"
block);

for (int i = 0; i < files.Length; i++)
{

if ((files.Substring(files.Length - 2) == ".c") ||
(files.Substring(files.Length - 2) == ".C")

|| (files.Substring(files.Length - 2) == ".h") ||
(files.Substring(files.Length - 2) == ".H"))


{

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

StreamReader reader = new StreamReader(fs);

str = reader.ReadToEnd();

int count = 0;

for (int k = 0; k < str.Length; k++)

{

if (str[k] == '\n')

count++;

}

reader.Close();

fs.Close();

fs.Dispose();

files = files.Substring(fpath.Length);

if (files.StartsWith("\\")) files = files.Substring(1);

newrow = new string[]{ files,(count+1).ToString() ,null };

dataGridView1.Rows.Add(newrow);

dataGridView1.Rows.HeaderCell.Value = i.ToString();

}

}


 
Hi,
Now I tried to run the program for 'C:\' folder after removing try-catch
block.
and I see the error at the last line.

if ((files.Substring(files.Length - 2) == ".c") ||
(files.Substring(files.Length - 2) == ".C")

|| (files.Substring(files.Length - 2) == ".h") ||
(files.Substring(files.Length - 2) == ".H"))


{

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

StreamReader reader = new StreamReader(fs);

str = reader.ReadToEnd();

int count = 0;

for (int k = 0; k < str.Length; k++)

{

if (str[k] == '\n')

count++;

}

reader.Close();

fs.Close();

fs.Dispose();

files = files.Substring(fpath.Length);

if (files.StartsWith("\\")) files = files.Substring(1);

newrow = new string[]{ files,(count+1).ToString() ,null };

dataGridView1.Rows.Add(newrow);

dataGridView1.Rows.HeaderCell.Value = i.ToString(); >>>>>>
Here i get the error;

}



Exception Detail;

System.ArgumentOutOfRangeException was unhandled
Message="Index was out of range. Must be non-negative and less than the
size of the collection.\r\nParameter name: index"
Source="mscorlib"
ParamName="index"
StackTrace:
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewRowCollection.SharedRow(Int32
rowIndex)
at System.Windows.Forms.DataGridViewRowCollection.get_Item(Int32
index)
at SatirSayaci.Form1.countbutton_Click(Object sender, EventArgs e) in
D:\code\csharp\SatirSayaci\SatirSayaci\Form1.cs:line 99
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at SatirSayaci.Program.Main() in
D:\code\csharp\SatirSayaci\SatirSayaci\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
Hi,


What is the value of i ?
What about dataGridView1.Rows.Count ?

My bet is that dataGridView1.Rows.Count < i

is dataGridView1 a dataview?

What if you do

dataGridView1.Rows[ dataGridView1.Rows.Count -1 ].HeaderCell.Value =
i.ToString();



--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Sirin Azazi said:
Hi,
Now I tried to run the program for 'C:\' folder after removing try-catch
block.
and I see the error at the last line.

if ((files.Substring(files.Length - 2) == ".c") ||
(files.Substring(files.Length - 2) == ".C")

|| (files.Substring(files.Length - 2) == ".h") ||
(files.Substring(files.Length - 2) == ".H"))


{

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

StreamReader reader = new StreamReader(fs);

str = reader.ReadToEnd();

int count = 0;

for (int k = 0; k < str.Length; k++)

{

if (str[k] == '\n')

count++;

}

reader.Close();

fs.Close();

fs.Dispose();

files = files.Substring(fpath.Length);

if (files.StartsWith("\\")) files = files.Substring(1);

newrow = new string[]{ files,(count+1).ToString() ,null };

dataGridView1.Rows.Add(newrow);

dataGridView1.Rows.HeaderCell.Value = i.ToString();
}



Exception Detail;

System.ArgumentOutOfRangeException was unhandled
Message="Index was out of range. Must be non-negative and less than the
size of the collection.\r\nParameter name: index"
Source="mscorlib"
ParamName="index"
StackTrace:
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewRowCollection.SharedRow(Int32
rowIndex)
at System.Windows.Forms.DataGridViewRowCollection.get_Item(Int32
index)
at SatirSayaci.Form1.countbutton_Click(Object sender, EventArgs e)
in D:\code\csharp\SatirSayaci\SatirSayaci\Form1.cs:line 99
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at SatirSayaci.Program.Main() in
D:\code\csharp\SatirSayaci\SatirSayaci\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
i changed it as,

dataGridView1.Rows[dataGridView1.Rows.Count - 2].HeaderCell.Value =
(dataGridView1.Rows.Count - 1).ToString();

and the problem was solved.

thank you.


haber said:
Hi,


What is the value of i ?
What about dataGridView1.Rows.Count ?

My bet is that dataGridView1.Rows.Count < i

is dataGridView1 a dataview?

What if you do

dataGridView1.Rows[ dataGridView1.Rows.Count -1 ].HeaderCell.Value =
i.ToString();



--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Sirin Azazi said:
Hi,
Now I tried to run the program for 'C:\' folder after removing try-catch
block.
and I see the error at the last line.

if ((files.Substring(files.Length - 2) == ".c") ||
(files.Substring(files.Length - 2) == ".C")

|| (files.Substring(files.Length - 2) == ".h") ||
(files.Substring(files.Length - 2) == ".H"))


{

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

StreamReader reader = new StreamReader(fs);

str = reader.ReadToEnd();

int count = 0;

for (int k = 0; k < str.Length; k++)

{

if (str[k] == '\n')

count++;

}

reader.Close();

fs.Close();

fs.Dispose();

files = files.Substring(fpath.Length);

if (files.StartsWith("\\")) files = files.Substring(1);

newrow = new string[]{ files,(count+1).ToString() ,null };

dataGridView1.Rows.Add(newrow);

dataGridView1.Rows.HeaderCell.Value = i.ToString(); >>>>>> Here i get
the error;

}



Exception Detail;

System.ArgumentOutOfRangeException was unhandled
Message="Index was out of range. Must be non-negative and less than the
size of the collection.\r\nParameter name: index"
Source="mscorlib"
ParamName="index"
StackTrace:
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewRowCollection.SharedRow(Int32
rowIndex)
at System.Windows.Forms.DataGridViewRowCollection.get_Item(Int32
index)
at SatirSayaci.Form1.countbutton_Click(Object sender, EventArgs e)
in D:\code\csharp\SatirSayaci\SatirSayaci\Form1.cs:line 99
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at SatirSayaci.Program.Main() in
D:\code\csharp\SatirSayaci\SatirSayaci\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top