Example #1
0
        } // createTempFile()

        public static string getTempDir()
        {
            string     result = "";
            FileStream fs     = null;
            // String tmpDirPath = System.getProperty("java.io.tmpdir");
            string tmp_file_path = Path.GetTempPath();

            if (tmp_file_path != null && !"".Equals(tmp_file_path))
            {
                fs = File.Open(tmp_file_path, FileMode.Create);
            }
            else
            {
                logger.debug("Could not determine tmpdir by using environment variable.");
                try
                {
                    // https://www.tutorialspoint.com/java/io/file_createtempfile_directory.htm
                    //File.createTempFile("foo", "bar");
                    tmp_file_path = ".foo.bar";
                    fs            = File.Open(tmp_file_path, FileMode.Create);
                    DirectoryInfo di = Directory.GetParent(tmp_file_path);
                    string        tmp_file_full_path = di.FullName + "\\" + tmp_file_path;
                    result = tmp_file_full_path;
                    try
                    {
                        File.Delete(tmp_file_full_path);
                    }
                    catch (Exception e)
                    {
                        logger.warn("Could not delete temp file '{}'", tmp_file_full_path);
                    }
                }
                catch (IOException e)
                {
                    logger.error("Could not create tempFile in order to find temporary dir", e);
                    try
                    {
                        DirectoryInfo di = Directory.CreateDirectory("metamodel.tmp.dir");
                    }
                    catch (Exception e2)
                    {
                        throw new InvalidOperationException("Could not create directory for temporary files: "
                                                            + e2.Message);
                    }

                    //result.deleteOnExit();
                    fs.Dispose();
                }
            }
            if (logger.isInfoEnabled())
            {
                logger.info("Using '{0}' as tmpdir.", result);
            }
            return(result);
        } // getTempDir()