Creating new file

G

Guest

How can i create a file when a filenotfound exception occurs ?
also when i try to access a file it says it is in use with another process. how do i know which process is using that file. I had exited all applications but then also the same error.
 
P

::P:e:s:c:e:::M:a:r:c:o::

How can i create a file when a filenotfound exception occurs ?
also when i try to access a file it says it is in use with another
process. how do i know which process is using that file. I had exited all
applications but then also the same error.
Dim ObjFile As IO.File

Dim strPath as string

If ObjFile.Exists(strPath ) Then

ObjFile.Create(strPath )

End If




--
Ciao

::M:a:r:c:blush::::p:e:s:c:e::
(e-mail address removed)
per contatti PVT elinimare NOSPAM
 
G

Guest

Dim fileNeeded As IO.File

Dim strPath as string

'Set path to expected location

strPath = "C:\Test.txt"
Try

If fileNeeded.Exists(strPath ) Then

fileNeeded.Open(strPath )

Else

fileNeeded.Create(strPath)

End If

Catch io As IOException

MsgBox io.Source

Finally

fileNeeded.Close()

End Try

This will handle the non-existent file - without throwing an exception. I hope that helps. Put up a sample of your code and I will see if I can provide a better example for you.
 
M

Morten Wennevik

No need to create a file object, you can use the static Exists and Create. Also, create if File.Exists returns false.

if(!File.Exists(filepath))
{
File.Create(filepath);
}
 
J

Jon Skeet [C# MVP]

Morten Wennevik said:
No need to create a file object, you can use the static Exists and Create.

Indeed, all of the File methods are static, and explicitly calling them
using File.Exists rather than (in VB.NET only) using a variable makes
it clearer to the reader that the methods *are* static.
 
G

Guest

Is this c#. I am in VS.Net with VB.net
But IO.File.Create works
and this is the shortest command
 
G

Guest

And what about the following error
I receive this error three times in a row when i click the menu item.
When the form loads this is the code which is executed:

DateTimePicker1.Value = Today
savefile = "F:\School\Studies Done\" + DateTimePicker1.Value + ".rtf"
RichTextBox1.LoadFile(savefile)

I have omitted some which i dont think is necessary because it is actually a leak of the source code. :))

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.IO.IOException: The process cannot access the file "F:\School\Studies Done\16-06-04.rtf" because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Windows.Forms.RichTextBox.SaveFile(String path, RichTextBoxStreamType fileType)
at System.Windows.Forms.RichTextBox.SaveFile(String path)
at Study.study_done_in_school.RichTextBox1_TextChanged(Object sender, EventArgs e) in F:\nickey\visual basic\Study\study_done_in_school.vb:line 237
at System.Windows.Forms.Control.OnTextChanged(EventArgs e)
at System.Windows.Forms.RichTextBox.OnTextChanged(EventArgs e)
at System.Windows.Forms.TextBoxBase.WmReflectCommand(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.WmReflectCommand(Message& m)
at System.Windows.Forms.RichTextBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///d:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
Study
Assembly Version: 1.0.1629.32547
Win32 Version: 1.0.1629.32547
CodeBase: file:///F:/nickey/visual%20basic/Study/bin/Study.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///d:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///d:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///d:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///d:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
 
J

Jon Skeet [C# MVP]

Is this c#. I am in VS.Net with VB.net
But IO.File.Create works
and this is the shortest command

Well, if you've got

Imports System.IO

at the top of your code, you can just use File.Create instead, which is
shorter. The best thing isn't the brevity, however - it's the fact that
it makes it obvious that Create is in fact a static method, not an
instance method.
 
M

Morten Wennevik

VB.NET equivalent would be something like

If Not File.Exists(filepath)
File.Create(filepath)
End If

I think, my VB is not the best.
 
M

Morten Wennevik

Yes, I forgot to add the FileStream to the code. File.Create returns a FileStream with the open file, so if you try to open it later it will cause an exception.

FileStream fs = null; // Dim fs As FileStream ?

if(!File.Exists(filepath))
{
fs = File.Create(filepath);
}

If you don't plan to use the file immediately call

fs.Close();
 
C

Chris Mullins

Jon Skeet said:
Well, if you've got

Imports System.IO

at the top of your code, you can just use File.Create instead, which is
shorter.

If you're in VB.NET, you could go one step further:

Imports System.IO.File

[...]

Delete(fileName)

The ability to imports a class in VS.NET is.... interesting and should
probably not be used. But knowing it's there is kinda neat...
 
J

Jon Skeet [C# MVP]

Chris Mullins said:
Well, if you've got

Imports System.IO

at the top of your code, you can just use File.Create instead, which is
shorter.

If you're in VB.NET, you could go one step further:

Imports System.IO.File

[...]

Delete(fileName)

The ability to imports a class in VS.NET is.... interesting and should
probably not be used. But knowing it's there is kinda neat...

I believe C# is getting that ability in v2. Like you, I believe it
should only rarely be used, but for classes which do a lot of
trigonometry, for instance, it could be very handy. I wouldn't use it
in a case like this though.
 

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

Top