2

I cannot write a file into a folder through some code in a VisualWebPart in a Sharepoint 2013 solution. I've given write permission to c:\myfolder to Everyone. The code I use to write is the following:

String timestamp = Utility.GetTimestamp(DateTime.Now);
            String folderpath = "" + @"c:\myfolder";
            String spyfile = ddlquery.SelectedValue + "_" + timestamp + ".txt";
            string spypath = folderpath + "\\" + spyfile;

            System.IO.File.WriteAllText(spypath, "Test test test...");

I've even tried wrapping the code with

  SPSecurity.RunWithElevatedPrivileges(delegate()
   {
     ....
   });

but no luck.

The ULS log I get is this:

Application error when access /SitePages/mypage.aspx, Error=Could not find a part of the path 'c:\myfolder\myfile_20151030095052084.txt'.  
 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, Boolean checkHost)    
 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)    
 at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)    
 at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost)    
 at  


System.IO.DirectoryNotFoundException: Could not find a part of the path 'c:\myfolder\myfile_20151030095052084.txt'.   
 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, Boolean checkHost)    
 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)    
 at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)    
 at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost)    
 at 

My code works on a development machine. Any ideas, about what could be wrong?

EDIT:

solved with the code below following Atish Dipongkor hint. Anyway, as strange as it may seems now I have two folders with the same name as the folder already existed.

   if (!System.IO.Directory.Exists(folderpath))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
   System.IO.Directory.CreateDirectory(folderpath);

            }
  • 1
    check the folder if it exits before writing file – Atish Dipongkor - MVP Oct 30 '15 at 9:13
  • Your piece of advice has really helped me (see edit). If you want to put it as an answer, I'll flag it – Gyonder Oct 30 '15 at 9:49
  • Added as answer. – Atish Dipongkor - MVP Oct 30 '15 at 9:54
1

Based on the comment, Exception arises when folder does not exit. So solution is following

if (!System.IO.Directory.Exists(folderpath))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
   System.IO.Directory.CreateDirectory(folderpath);

            }

Needs to check the folder if it exists before writing file.

1

In case this works on your DEV machine, but not in other environments, ensure the folder exists on all WFE servers of the farm (if there are many), as the code may be executed on any of them.

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.