C# 获取或设置文件属性

using System.ComponentModel;
using System.IO;
using System.Windows.Forms;

namespace WinFormFile
{
    public partial class FormProperty : Form
    {
        private PropertyGrid propertyFile;
        private OpenFileDialog openFile;

        public FormProperty()
        {
            InitializeComponent();
            this.AllowDrop = true;
            this.HelpButton = true;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.SizeGripStyle = SizeGripStyle.Hide;
            this.StartPosition = FormStartPosition.CenterScreen;
            openFile = new OpenFileDialog();
            openFile.Filter = "所有文件 (*.*)|*.*";
            propertyFile = new PropertyGrid();
            propertyFile.Dock = DockStyle.Fill;
            this.Controls.Add(propertyFile);
        }

        protected override void OnDragEnter(DragEventArgs e)
        {
            base.OnDragEnter(e);
            DataObject data = e.Data as DataObject;
            if (data.ContainsFileDropList())
            {
                string path = data.GetFileDropList()[0];
                if ((File.GetAttributes(path) & FileAttributes.Directory) == 0)
                    propertyFile.SelectedObject = new FileInfo(path);
                else
                    propertyFile.SelectedObject = new DirectoryInfo(path);
                this.Text = propertyFile.SelectedGridItem.Value as string;
            }
        }

        protected override void OnHelpButtonClicked(CancelEventArgs e)
        {
            base.OnHelpButtonClicked(e);
            e.Cancel = true;
            if (openFile.ShowDialog(this) == DialogResult.OK)
            {
                propertyFile.SelectedObject = new FileInfo(openFile.FileName);
                this.Text = propertyFile.SelectedGridItem.Value as string;
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要读取UG NX文件的属性,您可以使用UG NX软件提供的NXOpen API。NXOpen API是一组用于访问UG NX功能的C#编程接口。 以下是一个简单的示例代码,演示如何使用NXOpen API读取UG NX文件的属性。请注意,您需要安装UG NX软件,并在Visual Studio中添加对NXOpen.dll的引用才能使用此代码。 ```csharp using System; using NXOpen; class Program { static void Main(string[] args) { string filename = "C:\\path\\to\\your\\nx_file.prt"; // 初始化UG NX Session theSession = Session.GetSession(); // 打开文件 Part workPart = theSession.Parts.OpenBaseDisplay(filename); // 获取文件属性 string partName = workPart.Name; string partDescription = workPart.GetStringAttribute("Description"); DateTime partCreateDate = workPart.CreationTime; DateTime partModifiedDate = workPart.LastSavedTime; // 输出文件属性 Console.WriteLine("Part Name: " + partName); Console.WriteLine("Description: " + partDescription); Console.WriteLine("Creation Date: " + partCreateDate); Console.WriteLine("Modified Date: " + partModifiedDate); // 关闭文件 workPart.Close(BasePart.CloseWholeTree.True, null); } } ``` 在此示例代码中,我们使用`Session.GetSession()`方法初始化了UG NX,并使用`theSession.Parts.OpenBaseDisplay()`方法打开了一个UG NX文件。然后,我们使用`workPart.GetStringAttribute()`和`workPart.CreationTime`等方法获取了文件的属性。最后,我们使用`workPart.Close()`方法关闭了文件。 希望这个示例代码能够帮助您读取UG NX文件的属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值