c# 目录文件夹加前缀名

1 知识要点

1.1 修改文件名称

string srcFileName = @"D:/a.txt";
string destFileName = @"D:/b.txt";

if (System.IO.File.Exists(srcFileName))
            {
                System.IO.File.Move(srcFileName, destFileName);
            }

1.2 修改文件夹名称

string srcFolderPath = @"D:/1";
string destFolderPath = @"D:/6";

if (System.IO.Directory.Exists(srcFolderPath))
            {
                System.IO.Directory.Move(srcFolderPath, destFolderPath);
            }

2 今天应用目录文件夹加前缀名采用b修改文件夹名称

2.1 设计界面如下

2.2引用带地址行类插件代码(FolderBrowser.cs)如下

using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test
{
    public class FolderNameEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            FolderBrowserDialog browser = new FolderBrowserDialog();
            if (value != null)
            {
                browser.DirectoryPath = string.Format("{0}", value);
            }
            if (browser.ShowDialog(null) == DialogResult.OK)
                return browser.DirectoryPath;
            return value;
        }
    }


    #region FolderBrowserDialog Base

    /// <summary>
    /// Vista 样式的选择文件对话框的基类
    /// </summary>
    [Description("提供一个Vista样式的选择文件对话框")]
    [Editor(typeof(FolderNameEditor), typeof(UITypeEditor))]
    public class FolderBrowserDialog : Component
    {
        /// <summary>
        /// 初始化 FolderBrowser 的新实例
        /// </summary>
        public FolderBrowserDialog()
        {
        }

        #region Public Property
        /// <summary>
        /// 获取在 FolderBrowser 中选择的文件夹路径
        /// </summary>
        public string DirectoryPath { get; set; }
        /// <summary>
        /// 向用户显示 FolderBrowser 的对话框
        /// </summary>
        /// <param name="owner">任何实现 System.Windows.Forms.IWin32Window(表示将拥有模式对话框的顶级窗口)的对象。</param>
        /// <returns></returns>
        public DialogResult ShowDialog(IWin32Window owner)
        {
            IntPtr hwndOwner = owner != null ? owner.Handle : GetActiveWindow();
            IFileOpenDialog dialog = (IFileOpenDialog)new FileOpenDialog();
            try
            {
                IShellItem item;
                if (!string.IsNullOrEmpty(DirectoryPath))
                {
                    IntPtr idl;
                    uint atts = 0;
                    if (SHILCreateFromPath(DirectoryPath, out idl, ref atts) == 0)
                    {
                        if (SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0)
                        {
                            dialog.SetFolder(item);
                        }
                    }
                }
                dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM);
                uint hr = dialog.Show(hwndOwner);
                if (hr == ERROR_CANCELLED)
                    return DialogResult.Cancel;

                if (hr != 0)
                    return DialogResult.Abort;
                dialog.GetResult(out item);
                string path;
                item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path);
                DirectoryPath = path;
                return DialogResult.OK;
            }
            finally
            {
                Marshal.ReleaseComObject(dialog);
            }
        }
        #endregion

        #region BaseType
        [DllImport("shell32.dll")]
        private static extern int SHILCreateFromPath([MarshalAs(UnmanagedType.LPWStr)] string pszPath, out IntPtr ppIdl, ref uint rgflnOut);
        [DllImport("shell32.dll")]
        private static extern int SHCreateShellItem(IntPtr pidlParent, IntPtr psfParent, IntPtr pidl, out IShellItem ppsi);
        [DllImport("user32.dll")]
        private static extern IntPtr GetActiveWindow();
        private const uint ERROR_CANCELLED = 0x800704C7;
        [ComImport]
        [Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")]
        private class FileOpenDialog
        {
        }
        [ComImport]
        [Guid("42f85136-db7e-439c-85f1-e4075d135fc8")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        private interface IFileOpenDialog
        {
            [PreserveSig]
            uint Show([In] IntPtr parent); // IModalWindow
            void SetFileTypes();  // not fully defined
            void SetFileTypeIndex([In] uint iFileType);
            void GetFileTypeIndex(out uint piFileType);
            void Advise(); // not fully defined
            void Unadvise();
            void SetOptions([In] FOS fos);
            void GetOptions(out FOS pfos);
            void SetDefaultFolder(IShellItem psi);
            void SetFolder(IShellItem psi);
            void GetFolder(out IShellItem ppsi);
            void GetCurrentSelection(out IShellItem ppsi);
            void SetFileName([In, MarshalAs(UnmanagedType.LPWStr)] string pszName);
            void GetFileName([MarshalAs(UnmanagedType.LPWStr)] out string pszName);
            void SetTitle([In, MarshalAs(UnmanagedType.LPWStr)] string pszTitle);
            void SetOkButtonLabel([In, MarshalAs(UnmanagedType.LPWStr)] string pszText);
            void SetFileNameLabel([In, MarshalAs(UnmanagedType.LPWStr)] string pszLabel);
            void GetResult(out IShellItem ppsi);
            void AddPlace(IShellItem psi, int alignment);
            void SetDefaultExtension([In, MarshalAs(UnmanagedType.LPWStr)] string pszDefaultExtension);
            void Close(int hr);
            void SetClientGuid();  // not fully defined
            void ClearClientData();
            void SetFilter([MarshalAs(UnmanagedType.Interface)] IntPtr pFilter);
            void GetResults([MarshalAs(UnmanagedType.Interface)] out IntPtr ppenum); // not fully defined
            void GetSelectedItems([MarshalAs(UnmanagedType.Interface)] out IntPtr ppsai); // not fully defined
        }
        [ComImport]
        [Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        private interface IShellItem
        {
            void BindToHandler(); // not fully defined
            void GetParent(); // not fully defined
            void GetDisplayName([In] SIGDN sigdnName, [MarshalAs(UnmanagedType.LPWStr)] out string ppszName);
            void GetAttributes();  // not fully defined
            void Compare();  // not fully defined
        }
        private enum SIGDN : uint
        {
            SIGDN_DESKTOPABSOLUTEEDITING = 0x8004c000,
            SIGDN_DESKTOPABSOLUTEPARSING = 0x80028000,
            SIGDN_FILESYSPATH = 0x80058000,
            SIGDN_NORMALDISPLAY = 0,
            SIGDN_PARENTRELATIVE = 0x80080001,
            SIGDN_PARENTRELATIVEEDITING = 0x80031001,
            SIGDN_PARENTRELATIVEFORADDRESSBAR = 0x8007c001,
            SIGDN_PARENTRELATIVEPARSING = 0x80018001,
            SIGDN_URL = 0x80068000
        }
        [Flags]
        private enum FOS
        {
            FOS_ALLNONSTORAGEITEMS = 0x80,
            FOS_ALLOWMULTISELECT = 0x200,
            FOS_CREATEPROMPT = 0x2000,
            FOS_DEFAULTNOMINIMODE = 0x20000000,
            FOS_DONTADDTORECENT = 0x2000000,
            FOS_FILEMUSTEXIST = 0x1000,
            FOS_FORCEFILESYSTEM = 0x40,
            FOS_FORCESHOWHIDDEN = 0x10000000,
            FOS_HIDEMRUPLACES = 0x20000,
            FOS_HIDEPINNEDPLACES = 0x40000,
            FOS_NOCHANGEDIR = 8,
            FOS_NODEREFERENCELINKS = 0x100000,
            FOS_NOREADONLYRETURN = 0x8000,
            FOS_NOTESTFILECREATE = 0x10000,
            FOS_NOVALIDATE = 0x100,
            FOS_OVERWRITEPROMPT = 2,
            FOS_PATHMUSTEXIST = 0x800,
            FOS_PICKFOLDERS = 0x20,
            FOS_SHAREAWARE = 0x4000,
            FOS_STRICTFILETYPES = 4
        }
        #endregion
    }
    #endregion
}

2.3 按钮事件

2.3.1选择文件夹所在目录

public static string tpml;//定义全局变量
        public static string tpml2;
private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            if (path.ShowDialog(this) == DialogResult.OK)
            {
                tpml = path.DirectoryPath;
                tpml2 = textBox1.Text;
                Director(tpml);
            }
        }

效果如下

2.3.2 历遍所有目录

void Director(string dir)
        {

            DirectoryInfo d = new DirectoryInfo(dir);
            FileSystemInfo[] fsinfos = d.GetFileSystemInfos();

            List<string> lsTest2 = new List<string>(); //存储符合要求的文件的FiledInfo
            foreach (FileSystemInfo fsinfo in fsinfos)
            {
                if (fsinfo is DirectoryInfo)     //判断是否为文件夹
                {
                    string destFolderPath = fsinfo.FullName.Replace(fsinfo.Name, "")+textBox1.Text+ fsinfo.Name;
                    if (System.IO.Directory.Exists(fsinfo.FullName))
                    {
                        System.IO.Directory.Move(fsinfo.FullName, destFolderPath);
                    }
                   
                    Director(fsinfo.FullName);//递归调用
                }
               
            }
        }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在C#密和隐藏文件夹,可以使用以下步骤: 1. 选择要密和隐藏的文件夹。 2. 使用C#中的AES密算法对文件夹进行密。 3. 将密后的文件夹移动到一个隐藏文件夹中。 4. 使用C#中的文件系统对象隐藏该文件夹,使其在操作系统中不可见。 以下是一些示例代码,可以帮助你实现这些步骤: 使用AES密算法文件夹: ``` string password = "myPassword"; string folderPath = @"C:\MyFolder"; // Create a new AES object Aes aes = Aes.Create(); // Generate a random initialization vector byte[] iv = aes.IV; // Create a new encryption key using the password and IV Rfc2898DeriveBytes keyDerivation = new Rfc2898DeriveBytes(password, iv); byte[] key = keyDerivation.GetBytes(32); // Create a new encryption stream using (FileStream inputFileStream = new FileStream(folderPath, FileMode.Open)) using (FileStream outputFileStream = new FileStream(folderPath + ".encrypted", FileMode.Create)) using (ICryptoTransform encryptor = aes.CreateEncryptor(key, iv)) using (CryptoStream cryptoStream = new CryptoStream(outputFileStream, encryptor, CryptoStreamMode.Write)) { // Encrypt the input file stream and write it to the output file stream inputFileStream.CopyTo(cryptoStream); } // Delete the original folder Directory.Delete(folderPath, true); ``` 将密后的文件夹移动到一个隐藏文件夹中: ``` string hiddenFolderPath = @"C:\MyHiddenFolder"; // Create the hidden folder if it doesn't exist if (!Directory.Exists(hiddenFolderPath)) { Directory.CreateDirectory(hiddenFolderPath); } // Move the encrypted folder to the hidden folder string encryptedFolderPath = folderPath + ".encrypted"; File.Move(encryptedFolderPath, hiddenFolderPath + "\\" + Path.GetFileName(encryptedFolderPath)); ``` 使用文件系统对象隐藏该文件夹: ``` string folderName = Path.GetFileName(folderPath); string hiddenFolderPath = @"C:\MyHiddenFolder"; // Hide the folder by setting its attributes to hidden File.SetAttributes(hiddenFolderPath + "\\" + folderName, FileAttributes.Hidden); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值