コード例 #1
0
        // ----- METHODS -----

        /// <summary>
        /// Creates a new configuration file that can then be loaded normally.
        /// Note: This will NOT overwrite an existing file.
        /// </summary>
        /// <param name="folderPath">The path of the folder to create the configuration item.</param>
        /// <param name="name">The name of the configuration item to create.</param>
        /// <returns>True if the file was created or already exists, false if an error prevented it from being created.</returns>
        public static bool Create(string folderPath, string name)
        {
            string fullPath = folderPath + name + FILE_EXTENSION;

            if (!File.Exists(fullPath))
            {
                // Create the file.
                try
                {
                    // Create and close the file.
                    File file = new File(fullPath, false);
                    file.Close();
                    return(true);
                }
                catch
                {
                    // There was an error creating the file.
                    return(false);
                }
            }
            else
            {
                // The file already exists.
                return(true);
            }
        }
コード例 #2
0
        /// <summary>
        /// Writes a configuration item's value to a file.
        /// </summary>
        /// <param name="value">The value to write to the file.</param>
        /// <param name="log">The event log to log exceptions to. May be null for no logging.</param>
        /// <returns>True if the write was successful, false otherwise.</returns>
        protected bool Write(string value, EventLog.EventLog log)
        {
            // Write the value to the file.
            File file = null;

            try
            {
                file = new File(FilePath, false);
                return(file.WriteLine(value));
            }
            catch (Exception e)
            {
                // There was an error opening the file.
                LogException(e, log);
                return(false);
            }
            finally
            {
                // Close the file.
                if (file != null)
                {
                    file.Close();
                }
            }
        }
コード例 #3
0
ファイル: SharePointClient.cs プロジェクト: meng-her/Galactic
        /// <summary>
        /// Downloads a file from the SharePoint server with the specified relative URL.
        /// </summary>
        /// <param name="relativeUrl">The relative URL of the file to retrieve from the base of the web application containing it.</param>
        /// <param name="downloadPath">The path to the directory that the file should be downloaded to.</param>
        /// <returns>True if the file was downloaded, false otherwise.</returns>
        public bool DownloadFileByUrl(string relativeUrl, string downloadPath)
        {
            if (!string.IsNullOrWhiteSpace(relativeUrl) && Directory.Exists(downloadPath))
            {
                try
                {
                    // Get information about the file from SharePoint.
                    FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, relativeUrl);

                    // Get a reader for the file's contents.
                    BinaryReader reader = new BinaryReader(fileInfo.Stream);

                    // Get the segments of the relative path supplied.
                    string[] pathParts = relativeUrl.Split('/');

                    // Create a new file and get a stream of it for writing.
                    FileStream fileStream = GalFile.Create(downloadPath + pathParts[pathParts.Length - 1]);

                    // Get a writer from the file stream created above.
                    BinaryWriter writer = new BinaryWriter(fileStream);

                    // Create a buffer for reading / writing data that is the default size of NTFS file clusters.
                    byte[] buffer = new byte[NTFS_FILE_CLUSTER_SIZE_IN_BYTES];

                    // Tracks the number of bytes read from the current read.
                    int numBytesRead = reader.Read(buffer, 0, buffer.Length);

                    // Keep reading while there are bytes left in the file to read.
                    while (numBytesRead > 0)
                    {
                        // Write to the new file.
                        writer.Write(buffer, 0, numBytesRead);

                        // Read more bytes.
                        numBytesRead = reader.Read(buffer, 0, buffer.Length);
                    }

                    // The file was successfully downloaded.
                    return(true);
                }
                catch
                {
                    // There was an error downloading the file.
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
コード例 #4
0
 /// <summary>
 /// Deletes the configuration item.
 /// </summary>
 /// <returns>True if the item was deleted, false otherwise.</returns>
 public bool Delete()
 {
     // Check if the file exists.
     if (File.Exists(FilePath))
     {
         // The file exists.
         // Delete the file.
         return(File.Delete(FilePath));
     }
     else
     {
         // The file doesn't exist.
         return(true);
     }
 }
コード例 #5
0
        /// <summary>
        /// Retrieves the value of a configuration item from its file.
        /// </summary>
        /// <param name="log">The event log to log exceptions to. May be null for no logging.</param>
        /// <returns>The value of the configuration item or null if not found.</returns>
        protected string Get(EventLog.EventLog log)
        {
            File file = null;

            try
            {
                // Get the file.
                file = new File(FilePath, false, readOnly);

                // Check that the file exists.
                if (file.Exists())
                {
                    // Return the value of the item from the file.
                    return(file.ReadAllAsText());
                }
                else
                {
                    // The file doesn't exist. Return null.
                    return(null);
                }
            }
            catch (Exception e)
            {
                // There was an error opening or reading from the file.
                LogException(e, log);
                return(null);
            }
            finally
            {
                // Close the file.
                if (file != null)
                {
                    file.Close();
                }
            }
        }
コード例 #6
0
 /// <summary>
 /// Writes a configuration item's value to a file.
 /// </summary>
 /// <param name="value">The value to write to the file.</param>
 /// <param name="log">The event log to log exceptions to. May be null for no logging.</param>
 /// <returns>True if the write was successful, false otherwise.</returns>
 protected bool Write(string value, EventLog.EventLog log)
 {
     // Write the value to the file.
     File file = null;
     try
     {
         file = new File(FilePath, false);
         return file.WriteLine(value);
     }
     catch (Exception e)
     {
         // There was an error opening the file.
         LogException(e, log);
         return false;
     }
     finally
     {
         // Close the file.
         if (file != null)
         {
             file.Close();
         }
     }
 }
コード例 #7
0
        /// <summary>
        /// Retrieves the value of a configuration item from its file.
        /// </summary>
        /// <param name="log">The event log to log exceptions to. May be null for no logging.</param>
        /// <returns>The value of the configuration item or null if not found.</returns>
        protected string Get(EventLog.EventLog log)
        {
            File file = null;
            try
            {
                // Get the file.
                file = new File(FilePath, false, readOnly);

                // Check that the file exists.
                if (file.Exists())
                {
                    // Return the value of the item from the file.
                    return file.ReadAllAsText();
                }
                else
                {
                    // The file doesn't exist. Return null.
                    return null;
                }
            }
            catch (Exception e)
            {
                // There was an error opening or reading from the file.
                LogException(e, log);
                return null;
            }
            finally
            {
                // Close the file.
                if (file != null)
                {
                    file.Close();
                }
            }
        }