保存Active Document DTE VSIX扩展 [英] Save Active Document DTE VSIX extension
本文介绍了保存Active Document DTE VSIX扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建VS 2017扩展。我创建文件使用 ItemOperationd.NewFile
和
将一些内容添加到文件中但该文件存在于临时目录中我想将该文件保存到特定位置我该怎么办
这里是文件创建代码
dte 。 ItemOperations 。 NewFile ( @" General\Text File" , docName , EnvDTE 。 常数 。 vsViewKindTextView );
解决方案
嗨Gayan85,
>>我想将该文件保存到特定位置我该怎么办
如果你想要将文件保存到特殊位置,您可以使用File类来实现它。像这样:
string path = @" D:\ Data\MyTest。 txt" ;;
try
{
//删除文件(如果存在。
if(File.Exists(path))
{
//请注意,
//文件没有锁定,并且可能存在
//另一个进程可以执行
//在
之间的某些内容//对Exists的调用和删除。
File.Delete(路径);
}
//创建文件。
usi ng(FileStream fs = File.Create(path))
{
Byte [] info = new UTF8Encoding(true).GetBytes(" //这是文件中的一些文本。);
//向文件中添加一些信息。
fs.Write(info,0,info.Length);
}
//打开流并将其读回。
使用(StreamReader sr = File.OpenText(path))
{
string s ="" ;;
while((s = sr.ReadLine())!= null)
{
Console.WriteLine(s);
}
}
}
catch(例外情况)
{
Console.WriteLine(ex.ToString());
}祝你好运,
Cole Wu
i'm creating VS 2017 extension. i create file using ItemOperationd.NewFile
and
add some content to the file but that file exists on temp directory so i want to save that file in to specific location how can i do it
here is file create code
dte.ItemOperations.NewFile(@"General\Text File", docName, EnvDTE.Constants.vsViewKindTextView);
解决方案
Hi Gayan85,
>>i want to save that file in to specific location how can i do it
If you want to save the file into special location, you could use File class to achieve it. like this:
string path = @"D:\Data\MyTest.txt"; try { // Delete the file if it exists. if (File.Exists(path)) { // Note that no lock is put on the // file and the possibility exists // that another process could do // something with it between // the calls to Exists and Delete. File.Delete(path); } // Create the file. using (FileStream fs = File.Create(path)) { Byte[] info = new UTF8Encoding(true).GetBytes("//This is some text in the file."); // Add some information to the file. fs.Write(info, 0, info.Length); } // Open the stream and read it back. using (StreamReader sr = File.OpenText(path)) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); }Best regards,
Cole Wu
这篇关于保存Active Document DTE VSIX扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆