NuSpeccer.Package.SaveToFile C# (CSharp) Method

SaveToFile() public method

Serializes current Package object into file
public SaveToFile ( string fileName, System &exception ) : bool
fileName string full path of outupt xml file
exception System output Exception value if failed
return bool
        public virtual bool SaveToFile(string fileName, out System.Exception exception)
        {
            exception = null;
            try {
                SaveToFile(fileName);
                return true;
            }
            catch (System.Exception e) {
                exception = e;
                return false;
            }
        }

Same methods

Package::SaveToFile ( string fileName ) : void

Usage Example

示例#1
0
文件: Program.cs 项目: okb/NuSpeccer
        private static void Main(string[] args)
        {
            string filename = "";
            if (args.Length == 0)
            {
                Stream myStream = null;
                OpenFileDialog openFileDialog1 = new OpenFileDialog
                {
                    Filter = "dll files (*.dll)|*.dll|All files (*.*)|*.*",
                    FilterIndex = 1,
                    RestoreDirectory = true,
                    Multiselect = false
                };
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    filename = openFileDialog1.FileName;
                else
                    return;
            }
            else
                filename = Path.GetFullPath(args[0]);

            if (File.Exists(filename))
            {
                Package package = new Package();
                Assembly executingAssembly = Assembly.GetExecutingAssembly();

                using (Stream stream = executingAssembly
                    .GetManifestResourceStream(executingAssembly.GetName().Name + ".Package.nuspec"))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        var xml = reader.ReadToEnd();
                        package = Package.Deserialize(xml);
                    }
                }

                Assembly assembly = Assembly.LoadFile(filename);
                AssemblyInfoHelper helper = new AssemblyInfoHelper(assembly);
                IDictionary<string, string> allAttributes = helper.GetAllAttributes();

                allAttributes.Add("version", helper.FileVersion);
                allAttributes.Add("id", helper.AssemblyName);
                allAttributes.Add("authors", helper.Company);
                allAttributes.Add("owners", helper.Company);

                PropertyInfo[] properties = typeof(PackageMetadata).GetProperties(
                    BindingFlags.Public |
                    BindingFlags.DeclaredOnly |
                    BindingFlags.Instance);

                foreach (KeyValuePair<string, string> attribute in allAttributes)
                {
                    foreach (PropertyInfo propertyInfo in
                        properties.Where(propertyInfo => propertyInfo.Name.ToLower().Equals(attribute.Key.ToLower())).
                            Where(propertyInfo => propertyInfo.CanWrite))
                        propertyInfo.SetValue(package.Metadata, attribute.Value, null);
                }

                var version = helper.RuntimeVersion;
                package.Files.Add(new PackageFile
                    {
                        Src = helper.FilePath,
                        Target = "lib"
                    });

                var outFile = Path.GetFileName(filename.Replace(".dll", ".nuspec"));
                package.SaveToFile(outFile);
                Console.WriteLine("Done! " + outFile);
            }
            else
                Console.WriteLine("File does not exist!");
        }