Jump to content
  • Advertisement
Sign in to follow this  

Issues with creating save files with StreamWriter

This topic is 2793 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:
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:

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();

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

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!