有没有办法检查文件是否被锁定而不使用try/catch块?
现在,我所知道的唯一方法就是打开文件并抓住任何文件System.IO.IOException.
我刚刚看到一个关于try-catch的问题,人们(包括Jon Skeet)说空的拦截块是一个非常糟糕的主意?为什么这个?没有空的捕获不是错误的设计决定的情况吗?
我的意思是,例如,有时你想从某个地方(网络服务,数据库)获得一些额外的信息,你真的不在乎你是否会收到这些信息.所以你试图得到它,如果有什么事情发生,那没关系,我只会添加一个"catch(Exception ignored){}",这就是全部
类如Stream,StreamReader,StreamWriter等工具IDisposable界面.这意味着,我们可以Dispose()在这些类的对象上调用方法.他们还定义了一个public名为的方法Close().现在让我感到困惑的是,一旦我完成了对象,我该怎么称呼?如果我同时打电话怎么办?
我目前的代码是这样的:
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
using (StreamWriter writer = new StreamWriter(filename))
{
int chunkSize = 1024;
while (!reader.EndOfStream)
{
char[] buffer = new char[chunkSize];
int count = reader.Read(buffer, 0, chunkSize);
if (count != 0)
{
writer.Write(buffer, 0, count);
}
}
writer.Close();
}
reader.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我编写了using()构造,自动调用Dispose()每个对象的方法.但我也称之为Close()方法.这样对吗?
请告诉我使用流对象时的最佳做法.:-)
MSDN示例不使用using()构造,并且调用 …
在一个目录中创建文件(FileSystemWatcher_Created)时,我将其复制到另一个目录中.但是当我创建一个大(> 10MB)文件时,它无法复制文件,因为它已经开始复制,当文件尚未完成创建时...
这导致无法复制文件,因为它被另一个进程使用提高.;(
任何帮助?
class Program
{
static void Main(string[] args)
{
string path = @"D:\levan\FolderListenerTest\ListenedFolder";
FileSystemWatcher listener;
listener = new FileSystemWatcher(path);
listener.Created += new FileSystemEventHandler(listener_Created);
listener.EnableRaisingEvents = true;
while (Console.ReadLine() != "exit") ;
}
public static void listener_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine
(
"File Created:\n"
+ "ChangeType: " + e.ChangeType
+ "\nName: " + e.Name
+ "\nFullPath: " + e.FullPath
);
File.Copy(e.FullPath, @"D:\levan\FolderListenerTest\CopiedFilesFolder\" + e.Name);
Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud) 有什么控制可以递归调用一些东西吗?
从一个基本的测试程序,我得到一个超过18k的递归深度
这取决于stacksize ....
有没有办法设置一个大块的内存(也许是一个线程)与大量的堆栈,以增加递归深度?
我有时会遇到需要捕获异常的情况,如果它被抛出但从不对它做任何事情.换句话说,可能会发生异常,但如果发生异常则无关紧要.
我最近阅读了这篇关于类似内容的文章:http://c2.com/cgi/wiki?EmptyCatchClause
这个人谈论的评论如何
// should never occur
Run Code Online (Sandbox Code Playgroud)
是代码气味,不应该出现在代码中.然后他们去解释评论如何
// don't care if it happens
Run Code Online (Sandbox Code Playgroud)
完全不同,我自己遇到这样的情况.例如,在发送电子邮件时,我会执行与此类似的操作:
var addressCollection = new MailAddressCollection();
foreach (string address in addresses)
{
try
{
addressCollection.Add(address);
}
catch (Exception)
{
// Do nothing - if an invalid email occurs continue and try to add the rest
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可能认为这样做是个坏主意,因为您希望返回给用户并解释无法将一条或多条消息发送给收件人.但是,如果它只是一个CC地址怎么办?这不太重要,即使其中一个地址无效(可能只是一个错字),您仍然可能仍然希望发送消息.
所以我使用空挡块是对的还是有一个我不知道的更好的替代方案?
How do I wait for the file to be free so that ss.Save() can overwrite it with a new one? If I run this twice close together(ish), I get a generic GDI+ error.
///<summary>
/// Grabs a screen shot of the App and saves it to the C drive in jpg
///</summary>
private static String GetDesktopImage(DevExpress.XtraEditors.XtraForm whichForm)
{
Rectangle bounds = whichForm.Bounds;
// This solves my problem but creates a clutter issue
// var timeStamp = DateTime.Now.ToString("ddd-MMM-dd-yyyy-hh-mm-ss");
// var fileName …Run Code Online (Sandbox Code Playgroud) 可能重复:
等到文件在.NET中解锁
我有一个打开的文件,如.Doc或.txt,我必须等到用户关闭它.我已经尝试了这个,根据等到文件在.NET中解锁:
while (true)
{
try
{
using (FileStream Fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 100))
{
//the file is close
break;
}
}
catch (IOException)
{
//wait and retry
Thread.Sleep(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
这很好用,但是有可能找到没有try/catch的解决方案并处理异常?
很多时候,我们在尝试在Windows平台上编写文件时遇到错误,
"该进程无法访问文件'XXX',因为它正由另一个进程使用."
如何在写入文件之前检入C#,其他进程没有使用它?
c# ×9
.net ×4
file-io ×3
io ×3
file ×2
copy ×1
exception ×1
filelock ×1
filesystems ×1
idisposable ×1
ioexception ×1
recursion ×1
stack ×1
stream ×1
streamreader ×1
streamwriter ×1
try-catch ×1
windows ×1
winforms ×1