Sign in to follow this  

Issues with creating save files with StreamWriter

Recommended Posts

Hi, I've been trying to get back into coding, and started on a little project for a local games league to track scores, etc, but I seem to be having issues with using the StreamWriter method in C#. I can run and debug fine, but when I actually try to output a file, it throws an unhandled exception:
[code]System.IO.IOException was unhandled
Message=The process cannot access the file 'C:\**project filepath**\bin\Debug\enter league name' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append)
at WindowsFormsApplication1.CreateNewLeague.SaveNewLeague_Click(Object sender, EventArgs e) in C:\**project filepath**\\CreateNewLeague.cs:line 50
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(IntPtr 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 WindowsFormsApplication1.Program.Main() in C:\**project filepath**\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

The code I'm running that causes this is the following:

[code]private void SaveNewLeague_Click(object sender, EventArgs e)
SaveFileDialog NewLeagueSave = new SaveFileDialog();
NewLeagueSave.Filter = "XML File|*.xml|Text File|*.txt";
NewLeagueSave.Title = "Save League";
// set file name to NewLeagueName text box value
string NameString;
NameString = NewLeagueName.Text;
NewLeagueSave.FileName = NameString;
//this appears to do nothing.
if (NewLeagueSave.FileName != "")
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs =
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (NewLeagueSave.FilterIndex)
case 1:
case 2:

StreamWriter myStream = new StreamWriter(NewLeagueSave.FileName, true);

Now I've not really used C# before, and haven't touched any code for a couple of years, so I'm probably missing something really obvious here, but does anyone know why StreamWriter would be throwing an exception on the line "StreamWriter myStream = new StreamWriter(NewLeagueSave.FileName, true); " ?

Thanks in advance,

Share this post

Link to post
Share on other sites
You're attempting to open the file twice for writing:
System.IO.FileStream fs = (System.IO.FileStream)NewLeagueSave.OpenFile();[/code]
[code]StreamWriter myStream = new StreamWriter(NewLeagueSave.FileName, true);[/code]

Share this post

Link to post
Share on other sites
Ah, thanks - the perils of looking through multiple tutorials and MSDN at the same time. Got it working now (that is, not throwing any errors) using the StreamWriter, however, when I hit save on the save dialogue box, it doesn't actually create a file. Any ideas what's missing?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this