Is it possible to from my ASCX usercontrol to write to a text file either on the file system or in a sharepoint list? I tried using a streamwriter but I always get that it cant find the file Ive created.

  • definitely possible...can you expand on the problem and/or post your code because I'm not getting the correlation between writing out a file with a streamwriter and "I always get that it cant find the file I've created". Are you saying that you're able to create the file, but it says it can't find it once you try to write to it? either way, posting the snippet of code you're using to create/write might be helpful. – Rob D'Oria Aug 12 '11 at 15:00
  • @Rob I guess I just dont know where to put the file, and how to reference the file when I am using the StreamWriter – atrljoe Aug 12 '11 at 15:27
  • for starters, the default behavior of StreamWriter is to create the file if it doesn't exist assuming you're using the constructor that takes the path anyway. It's probably helpful to know the purpose of the file to give you a better recommendation on where it should go. – Rob D'Oria Aug 12 '11 at 15:37

Writing to the file system is very risky if you are in a load balanced environment as there is no guarantee that the code will be executed on the same server that wrote the file in the first place. However, even if you are on a single server environment, you will still hit deadlock/overwrite/permissions issues if two people happen to be accessing pages that contain your control at the same time.

Factor in the myriad of potential file system security issues and writing to the file system is significantly more work than it is worth.

It would be better to use a SharePoint list for this as it resolves both of the above issues. The exact code to use to write to the list would vary depending on what you are writing to the list and how (i.e. writing a file to a doclib versus attaching a file to a standard list item) In either case, I am pretty sure that you can use the MemoryStream object in order to keep everything in memory while you are preparing it to be written.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.