Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Need help saving files in a Windows standalone app

Discussion in 'Scripting' started by KappaTheKid, Sep 17, 2020.

  1. KappaTheKid

    KappaTheKid

    Joined:
    Sep 9, 2016
    Posts:
    6
    Hello, I'm currently working on a VR project and I'm in a point where I need to create a Pdf file with data from the project. The file creation is done using SharpPdf.

    Currently I am able to create the file and open it automatically within the Unity Editor, but the problem arises when I try to do it after building the project.

    First I set the name of the file that will be created:
    Code (CSharp):
    1. private string GetAttachName()
    2. {
    3.     var date = DateTime.Now.ToString();
    4.     string output = string.Empty;
    5.  
    6.     output += "StartupLab";
    7.     output += date.Split('/')[0];
    8.     output += date.Split('/')[1] + ".";
    9.     output += date.Split(' ')[1].Split(':')[0];
    10.     output += date.Split(' ')[1].Split(':')[1];
    11.     output += date.Split(' ')[1].Split(':')[2];
    12.     output += ".pdf";
    13.     return output;
    14. }
    This works fine. Then two string variables:
    Code (CSharp):
    1. _currFile = Application.streamingAssetsPath + "/" + AttachName;
    2. _targetFile = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + AttachName;
    _currFile is meant to be the path where the sharpPdf plugin outputs the file.
    _targetFile is the path where the file created will be moved to.

    Then, I start a coroutine called "CreatePdf", which sets up everything that will be inside the pdf file, and then at its end it calls:
    Code (CSharp):
    1. myDoc.createPDF(AttachName, true);
    Which is part of the sharpPdf plugin and contains the following:
    Code (CSharp):
    1. public void createPDF(string outputFile, bool toStreaming = false)
    2. {  
    3.     FileStream _myFileOut = null;;
    4.     if (toStreaming) outputFile = UnityEngine.Application.streamingAssetsPath + "/" + outputFile;
    5.     try {              
    6.         _myFileOut = new FileStream(outputFile, FileMode.Create);
    7.         createPDF(_myFileOut);              
    8.     } catch (IOException exIO) {
    9.         throw new pdfWritingErrorException("Error en la escritura del file",exIO);
    10.     } catch (pdfWritingErrorException exPDF) {
    11.         throw new pdfWritingErrorException("Error en la escritura del PDF",exPDF);
    12.     } finally {
    13.         if (_myFileOut != null) {
    14.             _myFileOut.Close();
    15.             _myFileOut = null;
    16.         }
    17.     }
    18. }
    And, finally, after all that is done, I do this:
    Code (CSharp):
    1. File.Move(_currFile, _targetFile);
    2. System.Diagnostics.Process.Start(_targetFile);
    Which works perfectly while in the Unity Editor, but not in the Windows standalone.

    I'm wondering what am I doing wrong or if I need to use different paths somewhere, so any help is appreciated.
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    What doesn't work? Do you get any errors? We need more info on what's actually broken