C# 获取文件名、目录、后缀、无后缀文件名、扩展名

  1. string filePath = "C:\\1.txt";

  2. string str = "获取文件的全路径:" + Path.GetFullPath(filePath); //-->C:1.txt

  3. str = "获取文件所在的目录:" + Path.GetDirectoryName(filePath); //-->C:

  4. Console.WriteLine(str);

  5. str = "获取文件的名称含有后缀:" + Path.GetFileName(filePath); //-->1.txt

  6. str = "获取文件的名称没有后缀:" + Path.GetFileNameWithoutExtension(filePath); //-->1

  7. str = "获取路径的后缀扩展名称:" + Path.GetExtension(filePath); //-->.txt

  8. str = "获取路径的根目录:" + Path.GetPathRoot(filePath); //-->C:\

转载于:https://www.cnblogs.com/LCLBook/p/10922217.html

### 前端实现文字居中方法 在前端开发中,通过 CSS 和 HTML 的配合可以轻松实现文字的水平和垂直居中效果。以下是几种常见的实现方式: #### 方法一:使用 `line-height` 和 `text-align` 当容器的高度固定时,可以通过设置 `line-height` 的值等于容器的高度来实现垂直居中,同时利用 `text-align: center` 来完成水平居中[^1]。 ```html <style> .container { height: 200px; width: 200px; border: 1px solid black; background-color: aliceblue; text-align: center; /* 水平居中 */ line-height: 200px; /* 垂直居中 */ } </style> <div class="container"> 单行文本居中显示 </div> ``` 需要注意的是,此方法仅适用于单行文本。如果文本超出一行,则会出现溢出的情况。 --- #### 方法二:使用 `flexbox` 现代浏览器广泛支持 Flexbox 布局模型,这是一种更灵活的方式,能够处理多行文本以及动态高度的内容居中问题。 ```html <style> .flex-container { display: flex; justify-content: center; /* 水平居中 */ align-items: center; /* 垂直居中 */ height: 200px; width: 200px; border: 1px solid black; background-color: aliceblue; } </style> <div class="flex-container"> 多行或多内容都可以完美居中<br>第二行测试 </div> ``` Flexbox 依赖固定的容器高度或宽度,因此非常适合响应式设计需求[^2]。 --- #### 方法三:使用绝对定位与变换属性 对于需要兼容旧版浏览器或者特定布局的情况下,可以采用绝对定位加 `transform` 的组合方案。 ```html <style> .positioned-container { position: relative; height: 200px; width: 200px; border: 1px solid black; background-color: aliceblue; } .center-text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); /* 调整到中心位置 */ } </style> <div class="positioned-container"> <p class="center-text">这是居中的文本</p> </div> ``` 这种方式同样适合多种复杂场景下的精确控制。 --- #### 方法四:基于表格单元格模拟 HTML 表格具有天然的文字自动居中特性,虽然现在较少直接使用 `<table>` 标签,但仍可通过样式模仿其行为。 ```html <style> .table-like-container { display: table-cell; vertical-align: middle; /* 垂直居中 */ text-align: center; /* 水平居中 */ height: 200px; width: 200px; border: 1px solid black; background-color: aliceblue; } </style> <div class="table-like-container"> 使用表格样式的居中方法 </div> ``` 尽管这种方法简单有效,但在实际项目中通常推荐优先考虑其他技术手段以保持语义化结构清晰。 --- ### 总结 以上介绍了四种同的文字居中解决方案,每一种都有各自适用范围及局限性。开发者应根据具体应用场景选择最合适的策略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值