Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Daniel Rogers 134 posts 712 karma points
    Jan 12, 2020 @ 15:07
    Daniel Rogers
    0

    Using umbraco version 8

    I need to store a string entered by user to a file. I needs to be stored in a set location from the root directory.

    Can someone please point me in the correct direction.

  • Graham Davis 110 posts 376 karma points
    Jan 12, 2020 @ 15:22
    Graham Davis
    0

    Hi Daniel,

    Use this method server side:

       public string WriteToFile(string filePath, string data)
        {
            try
            {
                try
                {
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                }
                catch (Exception ignore)
                {
                }
                File.AppendAllText(filePath, data);
                return "success";
            }
            catch (Exception ex) { return ex.Message; }
        }
    
  • Graham Davis 110 posts 376 karma points
    Jan 12, 2020 @ 15:26
    Graham Davis
    2

    Also, in case you need it, use this to get the root and folder path of your app:

    System.Web.Hosting.HostingEnvironment.MapPath("~/YouFolderPath")

  • Daniel Rogers 134 posts 712 karma points
    Jan 13, 2020 @ 03:44
    Daniel Rogers
    1

    Solved: Thanks Graham. Had to modify things slightly and here is the code and it works great.

    public string WriteToFile(string filePath, string data)
            {
    
                var file = new FileInfo(filePath);
                try
                {
                    try
                    {
                        if (file.Exists)
                        {
                            file.Delete();
                        }
                    }
                    catch (Exception ignore)
                    {
                    }
    
                    try
                    {
                        FileStream fs = file.Create();
    
                        StreamWriter w = new StreamWriter(fs);
                        w.Write(data);
                        w.Close();
                        fs.Close();
                    }
                    catch (Exception ignore)
                    {
                    }
                    return "success";
                }
                catch (Exception ex) { return ex.Message; }
            }
    
  • Graham Davis 110 posts 376 karma points
    Jan 13, 2020 @ 03:49
    Graham Davis
    2

    You're Welcome! Can I get a High 5?

Please Sign in or register to post replies

Write your reply to:

Draft