Я пытаюсь преобразовать длинный путь файла в короткий путь файла. Попробовал все подписи getShortPathName, доступные в Kernel32.dll, упомянутые ниже. Но все возвращают пустую строку вместо короткого пути. мой код. < /p>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
public partial class Form2 : Form
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string lpszLongPath,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder lpszShortPath,
uint cchBuffer);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(string lpszLongPath, char[] lpszShortPath, int cchBuffer);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW",
SetLastError = true)]
static extern int GetShortPathName(string pathName, System.Text.StringBuilder shortName, int
cbShortName);
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string longFilePath = @"C:\E\Authors files\GOLD\ddd Retail Leases\ddd Owned or Co-Owned Centres (including Property Income Fund Properties) – Managed by ddd\Garden City Booragoon (WA)\Specialty Leases & Deeds\Shop 12A\35120-#5337137, v1 _VICProduction1_ - Shop xxx - Garden City, Booragoon - D.C.K Australia Pty Ltd t_as Lovisa - Executed Deed of Assignment and Variation of Lease and Consent to Assignment - WorkSite Acrobat Integration.pdf";
string s1 = ShortPath(longFilePath);
string s2=ToShortPathName(longFilePath);
string s3 = GetShortPathName(longFilePath);
}
public static string ToShortPathName(string longName)
{
uint bufferSize = 256;
StringBuilder shortNameBuffer = new StringBuilder((int)bufferSize);
GetShortPathName(longName, shortNameBuffer, bufferSize);
return shortNameBuffer.ToString();
}
public string ShortPath(string longpath)
{
char[] buffer = new char[256];
GetShortPathName(longpath, buffer, buffer.Length);
return new string(buffer);
}
public static string GetShortPathName(string longFileName)
{
uint bufferSize = 256;
StringBuilder shortNameBuffer = new StringBuilder((int)bufferSize);
uint result = GetShortPathName(longFileName, shortNameBuffer, bufferSize);
return shortNameBuffer.ToString();
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/587 ... in-c-sharp
Сократить длинное имя файла с помощью kernel32.dll в c# ⇐ C#
Место общения программистов C#
-
Anonymous
1738278493
Anonymous
Я пытаюсь преобразовать длинный путь файла в короткий путь файла. Попробовал все подписи getShortPathName, доступные в Kernel32.dll, упомянутые ниже. Но все возвращают пустую строку вместо короткого пути. мой код. < /p>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
public partial class Form2 : Form
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string lpszLongPath,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder lpszShortPath,
uint cchBuffer);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(string lpszLongPath, char[] lpszShortPath, int cchBuffer);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW",
SetLastError = true)]
static extern int GetShortPathName(string pathName, System.Text.StringBuilder shortName, int
cbShortName);
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string longFilePath = @"C:\E\Authors files\GOLD\ddd Retail Leases\ddd Owned or Co-Owned Centres (including Property Income Fund Properties) – Managed by ddd\Garden City Booragoon (WA)\Specialty Leases & Deeds\Shop 12A\35120-#5337137, v1 _VICProduction1_ - Shop xxx - Garden City, Booragoon - D.C.K Australia Pty Ltd t_as Lovisa - Executed Deed of Assignment and Variation of Lease and Consent to Assignment - WorkSite Acrobat Integration.pdf";
string s1 = ShortPath(longFilePath);
string s2=ToShortPathName(longFilePath);
string s3 = GetShortPathName(longFilePath);
}
public static string ToShortPathName(string longName)
{
uint bufferSize = 256;
StringBuilder shortNameBuffer = new StringBuilder((int)bufferSize);
GetShortPathName(longName, shortNameBuffer, bufferSize);
return shortNameBuffer.ToString();
}
public string ShortPath(string longpath)
{
char[] buffer = new char[256];
GetShortPathName(longpath, buffer, buffer.Length);
return new string(buffer);
}
public static string GetShortPathName(string longFileName)
{
uint bufferSize = 256;
StringBuilder shortNameBuffer = new StringBuilder((int)bufferSize);
uint result = GetShortPathName(longFileName, shortNameBuffer, bufferSize);
return shortNameBuffer.ToString();
}
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/58747376/shorten-long-file-path-name-using-kernel32-dll-in-c-sharp[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия