Ejemplo n.º 1
0
        /// <summary>
        /// See if file can be opened for writing, return error if not
        /// </summary>
        /// <param name="path"></param>

        public static string CanWriteFile(
            string url)
        {
            try
            {
                bool existedOnEntry = FileExists(url);

                string          tempName = TempFile.GetTempFileName();
                IO.StreamWriter sw       = new IO.StreamWriter(tempName);
                sw.Close();

                CopyToSharePoint(tempName, url);
                IO.File.Delete(tempName);

                if (!existedOnEntry)                 // if didn't exist before then delete it
                {
                    DeleteFile(url);
                }
            }
            catch (Exception ex)             // catch "File Not Found" exception
            {
                return(ex.Message);
            }

            return("");
        }
Ejemplo n.º 2
0
/// <summary>
/// Copy file including SharePoint support
/// </summary>
/// <param name="?"></param>

        public static void CopyFile(
            string sourceFileName,
            string destFileName)
        {
            if (!SharePointUtil.IsSharePointName(sourceFileName) &&
                !SharePointUtil.IsSharePointName(destFileName))
            {             // both regular files
                File.Copy(sourceFileName, destFileName, true);
            }

            else if (SharePointUtil.IsSharePointName(sourceFileName) &&
                     SharePointUtil.IsSharePointName(destFileName))
            {             // both SharePoint
                string tempFile = TempFile.GetTempFileName();
                SharePointUtil.CopyFromSharePoint(sourceFileName, tempFile);
                SharePointUtil.CopyToSharePoint(tempFile, destFileName);
                File.Delete(tempFile);
            }

            else if (SharePointUtil.IsSharePointName(sourceFileName))             // source only is SharePoint
            {
                SharePointUtil.CopyFromSharePoint(sourceFileName, destFileName);
            }

            else             // dest only is SharePoint
            {
                SharePointUtil.CopyToSharePoint(sourceFileName, destFileName);
            }

            return;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Backup and replace a file
        /// </summary>
        /// <param name="destFile">Where the new file should go</param>
        /// <param name="newFile">New file to move to dest file</param>

        public static void ReplaceFile(
            string destFile,
            string newFile)
        {
            if (File.Exists(destFile))             // delete if exists
            {
                try
                {
                    System.IO.File.SetAttributes(destFile, FileAttributes.Normal); // set attributes to normal in case file is read only
                    File.Delete(destFile);                                         // remove any existing file
                }
                catch (Exception ex)
                {
                    try
                    {                                  // if can't delete try renaming
                        string tempFile = TempFile.GetTempFileName();
                        File.Move(destFile, tempFile); // move any existing dest temp file
                    }
                    catch (Exception ex2) { }          // no luck, continue on anyway
                }
            }

            try { File.Move(newFile, destFile); }             // move new to dest
            catch (Exception ex) { }

            return;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// If SharePoint file create a local cached copy of the file that can be read directly
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>

        public static string CacheLocalCopyIfSharePointFile(string path)
        {
            if (!IsSharePointName(path))
            {
                return(path);
            }

            string ext      = IO.Path.GetExtension(path);        // use same extension
            string tempFile = TempFile.GetTempFileName(ext);

            SharePointUtil.CopyFromSharePoint(path, tempFile);
            return(tempFile);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Extract the content of a zip file passed as a Base64 string
        /// </summary>
        /// <param name="b64String"></param>
        /// <param name="path"></param>
        /// <param name="filter"></param>

        public static void ExtractZipFileFromBase64String(
            string b64String,
            string path,
            FilterEntryMethod filter = null)
        {
            string tempZipFile = TempFile.GetTempFileName("zip");             // temp file for zip file

            FileUtil.WriteFileFromBase64String(b64String, tempZipFile);

            ZipFile zf = new ZipFile(tempZipFile);

            ExtractZipFile(zf, path, filter);

            FileUtil.DeleteFile(tempZipFile);
            return;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Extract the first file from a Zip file in the form of a byte array
        /// </summary>
        /// <param name="ba"></param>
        /// <param name="outputFile"></param>

        public static void ExtractFirstFile(
            byte[] ba,
            string outputFile)
        {
            string     tempZipFile = TempFile.GetTempFileName();         // temp file for zip file
            FileStream fs          = new FileStream(tempZipFile, FileMode.Create);

            fs.Write(ba, 0, ba.Length);
            fs.Close();

            ZipFile currentFile = new ZipFile(tempZipFile);

            foreach (ZipEntry entry in new EnumerationAdapter(new EnumerationMethod(currentFile.entries)))
            {             // extract first file from the .zip file
                if (entry.isDirectory())
                {
                    continue;
                }
                java.io.InputStream s = currentFile.getInputStream(entry);
                try
                {
                    java.io.FileOutputStream dest = new java.io.FileOutputStream(outputFile);
                    try
                    {
                        ZipUtils.CopyStream(s, dest);
                    }
                    finally
                    {
                        dest.close();
                    }
                }
                finally
                {
                    s.close();
                    FileUtil.DeleteFile(tempZipFile);
                }

                break;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Get a line of input from the user
        /// </summary>
        /// <param name="prompt"></param>
        /// <param name="title"></param>
        /// <param name="defaultText"></param>
        /// <param name="dictionary"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns>New input or null if cancelled</returns>

        public static string Show(
            string prompt,
            string title,
            string defaultText,
            List <string> dictionary,
            int width,
            int height)
        {
            string txt;

            if (Instance == null)
            {
                Instance = new InputBoxMx();
            }

            if (width <= 0)
            {
                width = 337;
            }
            if (height <= 0)
            {
                height = 153;
            }
            Instance.Width  = width;
            Instance.Height = height;

            if (WinFormsUtil.ControlMxConverter != null)
            {
                WinFormsUtil.ControlMxConverter(Instance, false);
            }


            if (prompt.Contains("</") || prompt.Contains("/>") || Lex.Contains(prompt, "<br>"))
            {             // display HTML prompt
                Instance.Prompt.Visible     = false;
                Instance.HtmlPrompt.Visible = true;
                string       htmlFile  = TempFile.GetTempFileName("html");
                StreamWriter sw        = new StreamWriter(htmlFile);
                int          backColor = ((Instance.BackColor.R * 256 + Instance.BackColor.G) * 256) + Instance.BackColor.B;
                string       hexColor  = String.Format("#{0:X}", backColor);
                sw.Write("<body " +
                         " topmargin='0' leftmargin='0' marginwidth='0' marginheight='0' hspace='0' vspace='0' " +
                         " style=\"font-size:8.5pt;font-family:'Tahoma';background-color:" + hexColor + "\">");
                sw.Write(prompt);
                sw.Write("</body>");
                sw.Close();
                Instance.HtmlPrompt.Navigate(htmlFile);
            }

            else             // display simple label prompt
            {
                Instance.HtmlPrompt.Visible = false;
                Instance.Prompt.Visible     = true;
                Instance.Prompt.Text        = prompt;
            }

            Instance.Text       = title;
            Instance.Input.Text = defaultText;

            Instance.Input.Properties.Items.Clear();
            if (dictionary != null)
            {
                Instance.Input.Properties.Items.AddRange(dictionary);
            }

            DialogResult dr = Instance.ShowDialog(Form.ActiveForm);             // (SessionManager.ActiveForm);

            if (dr == DialogResult.Cancel)
            {
                return(null);
            }
            else
            {
                return(Instance.Input.Text);
            }
        }