locked
Is there a permission I need to give in the program to write files? RRS feed

  • Question

  • I wrote my own class for opening and writing to files based off and using a lot of the C# file handling functionality. We learned to do simple things like this in java to get our heads around how things like IO worked.

     

    Any ways when I trye and create a file at path c:\ with content bla i get a access denied error in the console. What am I missing to say "Open a file or create a file here with this content no matter of the drives permissions?"

     

     


    Adrien....What?
    Wednesday, March 23, 2011 2:10 AM

Answers

  • //If the file doesnt exist we create it at root.   

    String tempPath = @"c:\";

    .....

    File.Create(tempPath);

    I am not sure you can create a file when a directory of the same name already exists. Looks like you forget to append the file name to the path,



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Wednesday, March 23, 2011 4:05 AM
  •   public class FileMethods
      {
        //If the file doesnt exist we create it at root.
        String tempPath = @"c:\";

        public Boolean open(String path)
        {
          try
          {
            if (File.Exists(path))
            {
              File.Open(path, FileMode.Open);
              return true;
            }
            else if (!File.Exists(path))
            {
              File.Create(tempPath);
              File.Open(path, FileMode.Open);
              return true;
            }
          }
          catch (IOException)
          {
            return false;
          }
          return false;
        }
    You need to involve the file name, i.e., the path should be the fullpath, to access the file.
    If the path in which the file name is not involved, is used, for example "C:\", this error will be thrown: Access to the path "C:\" is denied.
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 28, 2011 6:03 AM

All replies

  • Looks you do not have write permissions for C drive. Try creating a file in C drive manually. If you are not able to create the file, your program also can't create the file. On the other hand if you are able to create the file manually then please post your code.
    Please mark this post as answer if it solved your problem. Happy Programming !!!
    Wednesday, March 23, 2011 2:39 AM
  • So I can create files on My c drive manually. Keep in mind I am writing these files such as open, write and create from "scratch" ...So I could use the built in functions how ever I wanted to create my own simpler versions.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    using Phoniex.util.filehandeling.inter;
    
    namespace Phoniex.util.filehandeling
    {
      public class FileMethods
      {
        //If the file doesnt exist we create it at root.
        String tempPath = @"c:\";
    
        public Boolean open(String path)
        {
          try
          {
            if (File.Exists(path))
            {
              File.Open(path, FileMode.Open);
              return true;
            }
            else if (!File.Exists(path))
            {
              File.Create(tempPath);
              File.Open(path, FileMode.Open);
              return true;
            }
          }
          catch (IOException)
          {
            return false;
          }
    
          return false;
    
        }
    
        public String write(String write, String path)
        {
          String content;
          content = write;
    
          try
          {
            if (File.Exists(path))
            {
              File.AppendAllText(path, content);
            }
            else if (!File.Exists(path))
            {
              File.WriteAllText(path, content);
            }
          }
          catch (IOException)
          {
            throw new IOException();
          }
    
          return content;
        }
    
        public Boolean createFile(String path)
        {
          try
          {
            if (!File.Exists(path))
            {
              File.Create(path);
              return true;
            }
            else
            {
              throw new FileAlreadyExistsException("File already exists");
            }
          }
          catch (IOException)
          {
            return false;
          }
        }
      }
    }
    
    

    im sure there are other types of mistakes, How ever this builds with no issues

     

    They rely on the interface as shown below

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using System.IO;
    
    namespace Phoniex.util.filehandeling.inter
    {
      /// <summary>
      /// We want to create the basics of file
      /// operations with in the phoniex framework.
      /// we can build and extend upon this in later times.
      /// 
      /// the basic operations here are:
      /// 
      /// open, write and close.
      /// </summary>
      public interface FileOperations
      {
        /// <summary>
        /// Open a file under the path and name.
        /// </summary>
        /// <param name="path">path of the file</param>
        /// <returns></returns>
        Boolean open(String path);
    
        /// <summary>
        /// Creates a new file and then writes all the
        /// text to that file that the user enters.
        /// </summary>
        /// <param name="write">text to write</param>
        /// <param name="path">Path of file</param>
        /// <returns></returns>
        String write(String write, String path);
    
        /// <summary>
        /// Creates a file at specified path
        /// will not overwrite existing files instead
        /// will throw a FileAlreadyExistsException
        /// </summary>
        /// <param name="path">path to create file at.</param>
        /// <returns></returns>
        Boolean createFile(String path);
      }
    }
    
    
    Again the purpose of this was to not just "use" C#'s methods for opening, writing and creating files at chosen locations but to create simpler methods that would just "do the work." We would do things like this in java, including building out own array list to better understand the concepts being taught.


    Adrien....What?
    Wednesday, March 23, 2011 3:04 AM
  • //If the file doesnt exist we create it at root.   

    String tempPath = @"c:\";

    .....

    File.Create(tempPath);

    I am not sure you can create a file when a directory of the same name already exists. Looks like you forget to append the file name to the path,



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Wednesday, March 23, 2011 4:05 AM
  • i wasn't confused how to do that so I kinda left it out....LOL.

     

    the idea is that if file a.txt at path what ever exists. instead of throwing an error create that file on the temp path. I didnt include file name because I wanst sure if it was was just String fileName and then what to call because the File.Open() only takes a path and a mode.

     

    Also I cant even create files on the desktop because of what ever the error here is. If i was appending the file name to the path would that not cause issues or would I go:  String tempPath = @"c:\" + fileName? assuming that file exists at the location they were trying to originally create it at?

     

    all tempPath is for , as stated before, is for when you are trying to create a file at path a where that file already exists at path a. so we say ok file bla at path a exists so go create it at path b (still working out some kinks like telling the user we created file bla at path b instead of a.)

     

     


    Adrien....What?
    Wednesday, March 23, 2011 4:22 AM
  • Bump. Can Iget some help please?
    Adrien....What?
    Wednesday, March 23, 2011 12:18 PM
  • I suggest you to use Path.Combine to create a path. 

    Also if you need a temp path, you may want to use Path.GetTempPath to get a path you have write permission.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Thursday, March 24, 2011 5:45 AM
  • I think giving permission for the aspnet user will solve the issue
    George Mathew
    Thursday, March 24, 2011 7:18 AM
  •   public class FileMethods
      {
        //If the file doesnt exist we create it at root.
        String tempPath = @"c:\";

        public Boolean open(String path)
        {
          try
          {
            if (File.Exists(path))
            {
              File.Open(path, FileMode.Open);
              return true;
            }
            else if (!File.Exists(path))
            {
              File.Create(tempPath);
              File.Open(path, FileMode.Open);
              return true;
            }
          }
          catch (IOException)
          {
            return false;
          }
          return false;
        }
    You need to involve the file name, i.e., the path should be the fullpath, to access the file.
    If the path in which the file name is not involved, is used, for example "C:\", this error will be thrown: Access to the path "C:\" is denied.
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 28, 2011 6:03 AM