VS C# 2008 SP1
我创建了一个录制和播放音频的小应用程序。但是,我的应用程序需要将wave文件保存到用户计算机上的应用程序数据目录中。
mciSendString接受C样式字符串作为参数,并且必须是8.3格式。然而,我的问题是我不能保存它。奇怪的是,有时会失败,有时不会。然而,大多数时候都是失败的。但是,如果我直接保存到C盘,它第一次就能正常工作。我已经使用了3种不同的方法,我已经在下面编码。
失败时我得到的错误号是286。“该文件未保存。请确保您的系统有足够的磁盘空间或具有完整的网络连接”
非常感谢你的建议,
[DllImport("winmm.dll",CharSet=CharSet.Auto)]
private static extern uint mciSendString([MarshalAs(UnmanagedType.LPTStr)] string command,
StringBuilder returnValue,
int returnLength,
IntPtr winHandle);
[DllImport("winmm.dll", CharSet = CharSet.Auto)]
private static extern int mciGetErrorString(uint errorCode, StringBuilder errorText, int errorTextSize);
[DllImport("Kernel32.dll", CharSet=CharSet.Auto)]
private static extern int GetShortPathName([MarshalAs(UnmanagedType.LPTStr)] string longPath,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath,
int length);
// Stop recording
private void StopRecording()
{
// Save recorded voice
string shortPath = this.shortPathName();
string formatShortPath = string.Format("save recsound \"{0}\"", shortPath);
uint result = 0;
StringBuilder errorTest = new StringBuilder(256);
// C:\DOCUME~1\Steve\APPLIC~1\Test.wav
// Fails
result = mciSendString(string.Format("{0}", formatShortPath), null, 0, IntPtr.Zero);
mciGetErrorString(result, errorTest, errorTest.Length);
// command line convention - fails
result = mciSendString("save recsound \"C:\\DOCUME~1\\Steve\\APPLIC~1\\Test.wav\"", null, 0, IntPtr.Zero);
mciGetErrorString(result, errorTest, errorTest.Length);
// 8.3 short format - fails
result = mciSendString(@"save recsound C:\DOCUME~1\Steve\APPLIC~1\Test.wav", null, 0, IntPtr.Zero);
mciGetErrorString(result, errorTest, errorTest.Length);
// Save to C drive works everytime.
result = mciSendString(@"save recsound C:\Test.wav", null, 0, IntPtr.Zero);
mciGetErrorString(result, errorTest, errorTest.Length);
mciSendString("close recsound ", null, 0, IntPtr.Zero);
}
// Get the short path name so that the mciSendString can save the recorded wave file
private string shortPathName()
{
string shortPath = string.Empty;
long length = 0;
StringBuilder buffer = new StringBuilder(256);
// Get the length of the path
length = GetShortPathName(this.saveRecordingPath, buffer, 256);
shortPath = buffer.ToString();
return shortPath;
}发布于 2012-09-08 21:32:56
我在C drive和D drive中测试了它,只要目录路径不包含空格,它就可以工作。"C:\Test.wav"和"D:\mp3\test.wav"都可以工作,但"D:\mp4 folder\test.wav"不能工作。
发布于 2010-04-18 10:37:22
试试这个:
Directory.SetCurrentDirectory(directoryString);directoryString =“C:\mciSendString~1\Steve\APPLIC~1\”;string mciSendString(@“保存Test.wav",null,0,IntPtr.Zero);mciSendString("close recsound ",null,0,IntPtr.Zero);
发布于 2021-11-27 16:55:56
如果文件路径中有空格,则在文件名两边使用引号。在C#中,如果您使用filePath作为文件路径的变量,则使用以下代码:
msiSendString($"save recsound \"{filePath}\"", "", 0, 0)https://stackoverflow.com/questions/1148022
复制相似问题